Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用字符串访问特殊属性_Python - Fatal编程技术网

Python 使用字符串访问特殊属性

Python 使用字符串访问特殊属性,python,Python,我想表演一些像: lst = ['__len__()','__str__()', '__repr__()'] for i in lst: print(i, 'a'.i) 但它会抛出错误,因为“\uu len\uu()”是字符串。有什么方法可以把它转换成a吗?用它来完成你的任务 lst = ['__len__','__str__', '__repr__'] for i in lst: print(i, getattr('a', i)()) 用于完成您的任务 lst = ['__le

我想表演一些像:

lst = ['__len__()','__str__()', '__repr__()']

for i in lst:
  print(i, 'a'.i)
但它会抛出错误,因为
“\uu len\uu()”
是字符串。有什么方法可以把它转换成a吗?

用它来完成你的任务

lst = ['__len__','__str__', '__repr__']

for i in lst:
  print(i, getattr('a', i)())
用于完成您的任务

lst = ['__len__','__str__', '__repr__']

for i in lst:
  print(i, getattr('a', i)())
getattr('a',i)
是您要找的。
getattr('a',i)
是您要找的。