getattr(functiondemo, “run”) #获取run方法,存在打印出 方法的内存地址—<bound method function_demo.run of <main.function_demo object at 0x10244f320>>
getattr(functiondemo, “age”) #获取不存在的属性,报错如下: Traceback (most recent call last): File “/Users/liuhuiling/Desktop/MT_code/OpAPIDemo/conf/OPCommUtil.py”, line 39, in <module> res = getattr(functiondemo, “age”) AttributeError: ‘function_demo’ object has no attribute ‘age’
# a 与b 均为真,返回最后一个为真的值,返回b的值
a = 1
b = 2
print(a and b) >>>> 2
# c 与 d 有一个为假,返回第一个为假的值,返回c的值 c = 0 d = 2 print(c and d) >>>>>0
# e 与f 均为真,返回第一个 为真的值,返回e的结果 e = 1 f = 2 print(e or f) >>>>>>1
# g 与h 为假,返回第一个 为真的值,返回h的结果 g= ‘’ h=1 print(g or h) >>>>>1
类似三目表达式的用法:bool? a : b a ='first' b ='second' 1and a or b # 等价于 bool = true时的情况,a与b均为真 'first' >>>0and a or b # 等价于 bool = false时的情况 'second' >>> a ='' >>>1and a or b # a为假时,则出现问题 'second' >>>(1and[a]or[b])[0]# 安全用法,因为[a]不可能为假,至少有一个元素