标签:abs UI bool 自定义 bsp 3.3 调用 The www.
http://docs.python.org/3/library/functions.html 或者菜鸟中文资料
1-使用函数
abs(-20)#求绝对值 max(1,4,200,3,2) #求最大的值 int(12.34) #转成int float("12.34") #转成float str(1.23) #转成字符串 bool(1) #转成bool类型 bool(‘‘)
2-自定义函数
def my_abs(x): if not isinstance(x, (int, float)): return 222 if x >= 0: return x else: return -x
x,y=(111,222); x值是111,y值是222. 函数可直接返回tuple函数
3-函数的参数
3.1 默认参数, 定义默认参数要牢记一点:默认参数必须指向不变对象!
def sum(x, n=2): return x+n sum(5)#相当于调用power(5, 2): def enroll(name, gender, age=6, city=‘Beijing‘): print(‘city:‘, city) enroll(‘Adam‘, ‘M‘, city=‘Tianjin‘) #可以只传指定参数
3.2 可变参数
def calc(*numbers): #*表示可变 sum = 0 for n in numbers: sum = sum + n * n return sum calc(1,2,3) #参数调用 nums=[1,2,3] calc(*nums) #第二种方式
3.3 关键字参数
def person(name, age, **kw): print(‘name:‘, name, ‘age:‘, age, ‘other:‘, kw) extra = {‘city‘: ‘Beijing‘, ‘job‘: ‘Engineer‘} person(‘Jack‘, 24, **extra)
3.4命名关键字参数
def person(name, age, *args, city=‘beijing‘, job): print(name, age, args, city, job) person(‘xiaofeng‘,12,city=‘shenzhin‘,job=‘myjob‘) extra = {‘city‘: ‘Beijing‘, ‘job‘: ‘Engineer‘} person(‘xiaofeng‘,12,**extra)
3.5参数组合
def f1(a, b, c=0, *args, **kw): print(‘a =‘, a, ‘b =‘, b, ‘c =‘, c, ‘args =‘, args, ‘kw =‘, kw) args = (1, 2, 3, 4) kw = {‘d‘: 99, ‘x‘: ‘#‘} f1(*args, **kw) #a = 1 b = 2 c = 3 args = (4,) kw = {‘d‘: 99, ‘x‘: ‘#‘}
标签:abs UI bool 自定义 bsp 3.3 调用 The www.
原文地址:https://www.cnblogs.com/qinzb/p/9019879.html