Python:如何确定属性(按名称)是类属性还是实例属性?

2024-06-03 05:11:30 发布

您现在位置:Python中文网/ 问答频道 /正文

目标(在python2.7中):

检查任意对象,找到所有实例变量。但排除类变量。在

最终目标:

从没有提供有用的“str”实现的第三方类库打印对象的有用细节。(Maya的PythonAPI,版本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()

目前的解决方案:

^{pr2}$

用法示例:

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)

目标是提取相似的信息,对于那些我没有源代码(或者不想修改源代码)的类。这些类有许多类变量,这些变量隐藏了我所寻找的信息。在

问题:

如何检测“0”是从Vector继承的“类变量”,以便从输出中消除它?在

如果没有更好的方法,我将使用笨拙的方法:

printElements(Attributes(type(vec)))

列出对象类型的属性。可以对照“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

这将产生所需的结果:

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上的'Zero',它将包括:

# 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)

Tags: selfreturn属性defdescattributesclassother