2022.11.15 本学习内容总结于莫烦python:3.函数、类、模块
https://mofanpy.com/tutorials/python-basic/interactive-python/function
我常会重复写一些功能,比如查询文件时间,查询文件名字等等.后续我只需要引用到这个功能,多省事
def modify_name(filename):
filename += ".txt"
filename = "my_" + filename
print(filename)
modify_name("f1")
modify_name("f2")
my_f1.txt
my_f2.txt
实现了服用。这种服用形式也可以没有参数、有返回值:
def my_func():
filename = "f1"
ext = ".txt"
total_name = filename + ext
print(total_name)
my_func()
f1.txt
def modify_name(filename):
filename += ".txt"
filename = "my_" + filename
return filename
new_filename = modify_name("f1")
print(new_filename)
my_f1.txt
可以输入,可以有默认;我们在覆盖默认的时候,一定要指明覆盖的哪个默认数据 a=2
def f(x, a=1, b=1, c=0):
return a*x**2 + b*x + c*1
print(f(2, a=2))
print(f(2))
10
6
def modify_name():
filename = "f1.txt"
print("local filename:", filename)
modify_name()
print("global filename:", filename) # 这里会报错
这里会报错:因为filename
外面看不见,是个局部变量;
filename = "f1.txt"
def