help(getattr)
Help on built-in function getattr in module builtins:
getattr(...)
getattr(object, name[, default]) -> value
Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
When a default argument is given, it is returned when the attribute doesn't
exist; without it, an exception is raised in that case.
(END)
翻译理解一下就是,getattr用于获得object的name成员的值,如果不存在name这个成员,就会抛出exception;如果不想抛出exception,要么保证name成员存在,或者添加default参数,name成员不存在时就输出这个default。
name必须是str
name成员既可以是属性字段,也可以是函数方法
class Point():
def __init__(self,x,y,z,name):
self.x=x
self.y=y
self.z=z
self.name=name
def exection1(self):
self.x*=2
self.y*=4
self.z*=8
self.name=self.name+'_exec1'
return 'exection1_runs'
def exection2(self,**params):
self.x+=params['x']
self.y+=params['y']
self.z+=params['z']
self.name=self.name+'_'+params['name']
return 'exection2_runs'
p1=Point(1,2,3,'p1')
print('x =',getattr(p1,'x')) # p1.x
print('y =',getattr(p1,'y')) # p1.y
print('z =',getattr(p1,'z')) # p1.z
print('name =',getattr(p1,'name')) # p1.name
# x = 1
# y = 2
# z = 3
# name = p1
如果getattr()的name是一个函数,则会返回这个函数的信息,但是不执行函数
print(getattr(p1,'exection1'))
print('x =',getattr(p1,'x'))
print('y =',getattr(p1,'y'))
print('z =',getattr(p1,'z'))
print('name =',getattr(p1,'name'))
# >
# x = 1
# y = 2
# z = 3
# name = p1
可以看到函数exection1没有被执行,只是返回了exection1的信息
如果想调用这个函数,也就是执行这个函数,就要在后面添加**()**
print(getattr(p1,'exection1')()) # 增加()
print('x =',getattr(p1,'x'))
print('y =',getattr(p1,'y'))
print('z =',getattr(p1,'z'))
print('name =',getattr(p1,'name'))
# exection1_runs
# x = 2
# y = 8
# z = 24
# name = p1_exec1
p2=Point(1,2,3,'p2')
parameters={'x':10,'y':100,'z':1000,'name':'exec_2'}
print(getattr(p2,'exection2')(**parameters))
print('x =',getattr(p2,'x'))
print('y =',getattr(p2,'y'))
print('z =',getattr(p2,'z'))
print('name =',getattr(p2,'name'))
# exection2_runs
# x = 11
# y = 102
# z = 1003
# name = p2_exec_2
p3=Point(1,2,3,'p3')
print(getattr(p3,'x','x is no exist'))
# 1
print(getattr(p3,'x2','x2 is no exist'))
# x2 is no exist
print(getattr(p3,'x2'))
# Traceback (most recent call last):
# File "test.py", line 49, in
# print(getattr(p3,'x2'))
# AttributeError: 'Point' object has no attribute 'x2'
print(getattr(p3,'exection3','exection3 is not exist'))
# exection3 is not exist
print(getattr(p3,'exection3'))
# Traceback (most recent call last):
# File "test.py", line 52, in
# print(getattr(p3,'exection3'))
# AttributeError: 'Point' object has no attribute 'exection3'
class Point():
def __init__(self,x,y,z,name):
self.x=x
self.y=y
self.z=z
self.name=name
def exection1(self):
self.x*=2
self.y*=4
self.z*=8
self.name=self.name+'_exec1'
return 'exection1_runs'
def exection2(self,**params):
self.x+=params['x']
self.y+=params['y']
self.z+=params['z']
self.name=self.name+'_'+params['name']
return 'exection2_runs'
p1=Point(1,2,3,'p1')
print('x =',getattr(p1,'x'))
print('y =',getattr(p1,'y'))
print('z =',getattr(p1,'z'))
print('name =',getattr(p1,'name'))
print(getattr(p1,'exection1'))
print('x =',getattr(p1,'x'))
print('y =',getattr(p1,'y'))
print('z =',getattr(p1,'z'))
print('name =',getattr(p1,'name'))
p2=Point(1,2,3,'p2')
parameters={'x':10,'y':100,'z':1000,'name':'exec_2'}
print(getattr(p2,'exection2')(**parameters))
print('x =',getattr(p2,'x'))
print('y =',getattr(p2,'y'))
print('z =',getattr(p2,'z'))
print('name =',getattr(p2,'name'))
p3=Point(1,2,3,'p3')
print(getattr(p3,'x','x is no exist'))
print(getattr(p3,'x2','x2 is no exist'))
# print(getattr(p3,'x2'))
print(getattr(p3,'exection3','exection3 is not exist'))
# print(getattr(p3,'exection3'))