提问人:Chris Bunch 提问时间:9/21/2008 最后编辑:ROMANIA_engineerChris Bunch 更新时间:1/18/2023 访问量:207344
如何在Python中获取实例变量?
How to get instance variables in Python?
问:
Python 中是否有内置方法可以获取所有类实例变量的数组?例如,如果我有以下代码:
class hi:
def __init__(self):
self.ii = "foo"
self.kk = "bar"
我有没有办法做到这一点:
>>> mystery_method(hi)
["ii", "kk"]
编辑:我最初错误地要求类变量。
答:
每个对象都有一个变量,其中包含所有变量及其值。__dict__
试试这个
>>> hi_obj = hi()
>>> hi_obj.__dict__.keys()
输出
dict_keys(['ii', 'kk'])
评论
通常,您不能只获得一个类的实例属性,至少在不实例化该类的情况下是无法获得的。但是,您可以获取给定实例的实例属性,或获取给定类的类属性。请参阅“检查”模块。您无法获取实例属性的列表,因为实例实际上可以将任何内容作为属性,并且 - 如您的示例中 - 创建它们的正常方法是在 __init__ 方法中分配给它们。
例外情况是,如果您的类使用槽,槽是类允许实例具有的固定属性列表。插槽在 http://www.python.org/2.2.3/descrintro.html 中进行了解释,但插槽存在各种陷阱;它们会影响内存布局,因此多重继承可能会有问题,并且继承通常也必须考虑插槽。
评论
您还可以使用以下命令测试对象是否具有特定变量:
>>> hi_obj = hi()
>>> hasattr(hi_obj, "some attribute")
False
>>> hasattr(hi_obj, "ii")
True
>>> hasattr(hi_obj, "kk")
True
您的示例显示的是“实例变量”,而不是真正的类变量。
查找类变量以及其他类成员,如成员函数和包含模块。hi_obj.__class__.__dict__.items()
class Hi( object ):
class_var = ( 23, 'skidoo' ) # class variable
def __init__( self ):
self.ii = "foo" # instance variable
self.jj = "bar"
类变量由类的所有实例共享。
评论
list(filter(None,[ i[0] if not i[0].startswith('_') else None for i in h.__class__.__dict__.items()]))
本质上
使用 vars()
class Foo(object):
def __init__(self):
self.a = 1
self.b = 2
vars(Foo()) #==> {'a': 1, 'b': 2}
vars(Foo()).keys() #==> ['a', 'b']
评论
__method
__method__
__method__
非常丑陋,我认为它们以这种方式命名是为了试图阻止人们使用它们,除非绝对必要。在本例中,我们有替代的 vars()。
__str__
__method__
str(something)
建议
>>> print vars.__doc__
vars([object]) -> dictionary
Without arguments, equivalent to locals().
With an argument, equivalent to object.__dict__.
换句话说,它本质上只是包装__dict__
虽然不是 OP 问题的直接答案,但有一种非常甜蜜的方法可以找出函数中哪些变量在作用域内。请看这段代码:
>>> def f(x, y):
z = x**2 + y**2
sqrt_z = z**.5
return sqrt_z
>>> f.func_code.co_varnames
('x', 'y', 'z', 'sqrt_z')
>>>
func_code属性中有各种有趣的东西。它允许你做一些很酷的事情。以下是我如何使用它的一个例子:
def exec_command(self, cmd, msg, sig):
def message(msg):
a = self.link.process(self.link.recieved_message(msg))
self.exec_command(*a)
def error(msg):
self.printer.printInfo(msg)
def set_usrlist(msg):
self.client.connected_users = msg
def chatmessage(msg):
self.printer.printInfo(msg)
if not locals().has_key(cmd): return
cmd = locals()[cmd]
try:
if 'sig' in cmd.func_code.co_varnames and \
'msg' in cmd.func_code.co_varnames:
cmd(msg, sig)
elif 'msg' in cmd.func_code.co_varnames:
cmd(msg)
else:
cmd()
except Exception, e:
print '\n-----------ERROR-----------'
print 'error: ', e
print 'Error proccessing: ', cmd.__name__
print 'Message: ', msg
print 'Sig: ', sig
print '-----------ERROR-----------\n'
Vars() 和 dict 方法都适用于 OP 发布的示例,但它们不适用于“松散”定义的对象,例如:
class foo:
a = 'foo'
b = 'bar'
若要打印所有不可调用的属性,可以使用以下函数:
def printVars(object):
for i in [v for v in dir(object) if not callable(getattr(object,v))]:
print '\n%s:' % i
exec('print object.%s\n\n') % i
评论
exec('print object.%s\n\n') % i
应该写成print getattr(object, i)
建立在 dmark 的答案之上,得到以下内容,如果你想要 sprintf 的等价物,这很有用,希望能帮助到某人......
def sprint(object):
result = ''
for i in [v for v in dir(object) if not callable(getattr(object, v)) and v[0] != '_']:
result += '\n%s:' % i + str(getattr(object, i, ''))
return result
有时,您希望根据公共/私有变量筛选列表。例如
def pub_vars(self):
"""Gives the variable names of our instance we want to expose
"""
return [k for k in vars(self) if not k.startswith('_')]
首先,您需要检查类,然后检查函数的字节码,然后复制字节码,最后使用 __code__.co_varnames
。这很棘手,因为某些类使用构
造函数(如 types 模块中的构造函数)创建它们的方法。我将在 GitHub 上为它提供代码。
基于 Ethan Joffe 的回答
def print_inspect(obj):
print(f"{type(obj)}\n")
var_names = [attr for attr in dir(obj) if not callable(getattr(obj, attr)) and not attr.startswith("__")]
for v in var_names:
print(f"\tself.{v} = {getattr(obj, v)}\n")
评论