0
我正在生成類變量列表,我需要打印這些變量的值。如何從python列表中獲取變量名稱的值,例如
class MYClass(object):
a = '1'
b = '20'
c = 'hello'
def as_list(self):
return [attr for attr in dir(MYClass()) if not callable(attr) and not attr.startswith("__") and not attr == 'as_list']
c = MYClass()
print c.as_list()
上面的代碼將導致[ 'A', 'B', 'C'],但我需要的值作爲[ '1', '20', '你好']
不是'dir(self)'而不是'dir(MYClass())'更合適嗎? – Leon
其實這是靜態方法,所以自己不能使用,粘貼在這裏我對代碼示例做了一些小的修改 – santosh