Python:如何确定属性(按名称)是类还是实例属性? [英] Python: How determine if attribute (by name) is class or instance attribute?
问题描述
目标(在Python 2.7中):
Goal (in Python 2.7):
检查任意对象,找到所有实例变量.但是排除类变量.
Inspecting an arbitrary object, find all of the instance variables. But exclude class variables.
最终目标:
从没有提供有用的"str"实现的第三方类库中打印对象的有用细节. (Maya的Python API版本1,这是一个简单的SWIG包装器. 不使用版本2,因为我正在从版本1的示例中学习.)
Print useful details of an object, from a third-party class library that doesn't provide a useful "str" implementation. (Maya's Python API, version 1, which is a simple SWIG wrapper. not using version 2, because I'm learning from some version 1 examples.)
示例类:
# ---------- class Vector ----------
class Vector(object):
def __init__(self, x=0.0, y=0.0, z=0.0):
self.x, self.y, self.z = x, y, z
# Provide useful info for 'repr(self)', 'str(self)', and 'print self'.
def __repr__(self):
return 'Vector({0}, {1}, {2})'.format(self.x, self.y, self.z)
# math operators
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y, self.z + other.z)
# a simple method
def ApproximateLength(self):
return self.x + self.y + self.z
# list/sequence/iterator support.
def tolist(self):
return [self.x, self.y, self.z]
def __len__(self):
return 3
# No need for "next(self)", because we create a list, use its iterator.
def __iter__(self):
return iter(self.tolist())
# class variable
Vector.Zero = Vector()
到目前为止的解决方案:
Solution so far:
import inspect
import types
def printElements(ob):
for x in ob: print x
# Excludes 'internal' names (start with '__').
def Public(name):
return not name.startswith('__')
def Attributes(ob):
# Exclude methods.
attributes = inspect.getmembers(ob, lambda member: not inspect.ismethod(member))
# Exclude 'internal' names.
publicAttributes = filter(lambda desc: Public(desc[0]), attributes)
return publicAttributes
示例用法:
vec = Vector(1.0, 2.0, 3.0)
printElements(Attributes(vec))
输出:
('Zero', Vector(0.0, 0.0, 0.0))
('x', 1.0)
('y', 2.0)
('z', 3.0)
此类的打印效果很好:
print vec
=>
Vector(1.0, 2.0, 3.0)
目标是为我没有来源(或不想修改其来源)的类提取类似的信息.这些类具有许多类变量,这些变量会掩埋我要查找的信息.
The goal is to extract similar information, for classes that I don't have source to (or don't want to modify the source of). Those classes have many class variables, which bury the information I seek.
问题:
如何检测零"是从Vector继承的类变量",以将其从输出中消除?
How detect that 'Zero' is a "class variable", inherited from Vector, to eliminate it from the output?
笨拙的方法,如果没有更好的方法,我会使用:
Clumsy approach I will use if no better way:
printElements(Attributes(type(vec)))
列出有关对象类型的属性.可以针对"type(vec)"的属性测试"vec"的每个属性,不包括任何匹配的属性.我不在乎在类和实例上都存在相同的命名属性的细微可能性.这样就可以满足我的要求.
lists the attributes on the object's type. Could test each attribute of "vec" against the attributes of "type(vec)", excluding any that match. I don't care about the subtle possibility that the same named attribute exists on both class and instance. So this would satisfy my requirements.
但是,这看起来很笨拙.有没有更直接的方法来确定属性是否从类继承?
However, that seems clumsy. Is there a more direct way to determine whether the attribute is inherited from the class?
结合乔兰的答案:
def IsClassVar(self, attrName):
return hasattr(self.__class__, attrName)
def Attributes(ob):
....
publicAttributes = filter(lambda desc: Public(desc[0]), attributes)
# Exclude 'class' variables.
# NOTE: This does not attempt to detect whether the instance variable is different than the class variable.
publicAttributes = filter(lambda desc: not isClassVar(ob, desc[0]), publicAttributes)
return publicAttributes
这给出了预期的结果:
printElements(Attributes(vec))
=>
('x', 1.0)
('y', 2.0)
('z', 3.0)
或者,要检测实例变量重写类变量,请执行以下操作:
def IsClassVar(self, attrName):
return hasattr(self.__class__, attrName)
# REQUIRE attrName already known to be supported by self.
# But just in case, return False if exception, so will be skipped.
def IsNotSameAsClassVar(self, attrName):
try:
if not IsClassVar(self, attrName):
return True
# If it has different value than class' attribute, it is on the instance.
return getattr(self, attrName) is not getattr(self.__class__, attrName)
except:
return False
def Attributes(ob):
....
publicAttributes = filter(lambda desc: Public(desc[0]), attributes)
# Exclude 'class' variables.
# More complete solution.
publicAttributes = filter(lambda desc: IsNotSameAsClassVar(ob, desc[0]), publicAttributes)
return publicAttributes
现在,如果我们在vec上覆盖零",它将包括在内:
Now if we override 'Zero' on vec, it gets included:
# Probably a bad idea, but showing the principle.
vec.Zero = "Surprise!"
然后:
print vec.Zero
print Vector.Zero
=>
Surprise!
Vector(0.0, 0.0, 0.0)
并且:
printElements(Attributes(vec))
=>
('Zero', 'Surprise!')
('x', 1.0)
('y', 2.0)
('z', 3.0)
推荐答案
类似的方法可能有效
def isClassVar(self,varname):
return hasattr(self.__class__,varname)
...
vec.isClassVar("Zero")
请注意,这并不一定意味着它是一个实例变量...只是不是一个类变量
note that this does not necessarily mean it is an instance variable ... just that is is not a class variable
这篇关于Python:如何确定属性(按名称)是类还是实例属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!