def test(name,age=20):#age可以默认
print(name+str(age))
test(name="c",age=30)
test("a")
#传入元组,参数随便写,将参数全部封装到元组中
def tup(*tupl):
print(tupl)
tup('x','s','c')
#限制参数的传入,默认读取第一个,后面的全部放入元组
def tup1(size,*tup2):
print(size)
print(tup2)
tup1(15,"ds",52,"dd")
#也可传入字典
dir1={"name":"小明","age":23}
def tup2(size,**dir):
print(size)
print(dir)
tup2(23,name='大明',age=23)
tup2(24,**dir1)
Python中变量都存在命名空间中,可用locals获取空间里面东西,以字典形式存储,globals获取全局的
def local():
a = 1
scope=locals()
print(scope)
local()
#{'a': 1}
匿名函数lambda用来创造简单的函数
#匿名函数
def fun(a,b):
return a+b
print(fun(4,1))
#可用lambda
lambda a,b:a+b
fn=lambda a,b:a+b
print(fn(2,4) )
Python中的反射
#Python中的反射
class fun():
def func(self):
coun=0
print(coun)
a=getattr(fun,"func")
print(a)