Too*_*eve 2 python attributes class inspect
目标(在 Python 2.7 中):
检查任意对象,找到所有实例变量。但排除类变量。
最终目标:
从不提供有用的“str”实现的第三方类库打印对象的有用细节。(Maya 的 Python API,版本 1,它是一个简单的 SWIG 包装器。不使用版本 2,因为我正在从一些版本 1 的示例中学习。)
示例类:
# ---------- 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()
Run Code Online (Sandbox Code Playgroud)
到目前为止的解决方案:
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
Run Code Online (Sandbox Code Playgroud)
用法示例:
vec = Vector(1.0, 2.0, 3.0)
printElements(Attributes(vec))
Run Code Online (Sandbox Code Playgroud)
输出:
('Zero', Vector(0.0, 0.0, 0.0))
('x', 1.0)
('y', 2.0)
('z', 3.0)
Run Code Online (Sandbox Code Playgroud)
这个类确实打印得很好:
print vec
Run Code Online (Sandbox Code Playgroud)
=>
Vector(1.0, 2.0, 3.0)
Run Code Online (Sandbox Code Playgroud)
目标是为我没有来源(或不想修改其来源)的类提取类似的信息。这些类有许多类变量,它们隐藏了我寻找的信息。
题:
如何检测“零”是从 Vector 继承的“类变量”,以将其从输出中消除?
如果没有更好的方法,我将使用笨拙的方法:
printElements(Attributes(type(vec)))
Run Code Online (Sandbox Code Playgroud)
列出对象类型的属性。可以针对“type(vec)”的属性测试“vec”的每个属性,排除任何匹配的属性。我不关心在类和实例上都存在相同命名属性的微妙可能性。所以这将满足我的要求。
然而,这似乎很笨拙。有没有更直接的方法来判断属性是否继承自类?
编辑:纳入乔兰的回答:
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
Run Code Online (Sandbox Code Playgroud)
这给出了所需的结果:
printElements(Attributes(vec))
Run Code Online (Sandbox Code Playgroud)
=>
('x', 1.0)
('y', 2.0)
('z', 3.0)
Run Code Online (Sandbox Code Playgroud)
替代方法,要检测覆盖类变量的实例变量:
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
Run Code Online (Sandbox Code Playgroud)
现在,如果我们在 vec 上覆盖“零”,它将被包含在内:
# Probably a bad idea, but showing the principle.
vec.Zero = "Surprise!"
Run Code Online (Sandbox Code Playgroud)
然后:
print vec.Zero
print Vector.Zero
Run Code Online (Sandbox Code Playgroud)
=>
Surprise!
Vector(0.0, 0.0, 0.0)
Run Code Online (Sandbox Code Playgroud)
和:
printElements(Attributes(vec))
Run Code Online (Sandbox Code Playgroud)
=>
('Zero', 'Surprise!')
('x', 1.0)
('y', 2.0)
('z', 3.0)
Run Code Online (Sandbox Code Playgroud)
这样的事情可能会奏效
def isClassVar(self,varname):
return hasattr(self.__class__,varname)
...
vec.isClassVar("Zero")
Run Code Online (Sandbox Code Playgroud)
请注意,这并不一定意味着它是一个实例变量......只是它不是一个类变量
| 归档时间: |
|
| 查看次数: |
1988 次 |
| 最近记录: |