在python中访问类实例属性的最佳方法

在python中访问类实例属性的最佳方法,python,class,Python,Class,我有一门课,比如说: #!/usr/bin/env python class testclass(object): def __init__(self, a, b, c): self.__attr1 = a self.__attr2 = b self.__attr3 = c def meth(self): ## run some checks on self.__attr1, self.__attr2, self

我有一门课,比如说:

#!/usr/bin/env python

class testclass(object):
    def __init__(self, a, b, c):
        self.__attr1 = a
        self.__attr2 = b
        self.__attr3 = c

    def meth(self):
        ## run some checks on self.__attr1, self.__attr2, self.__attr3 
                    ## How to do that?  

if __name__ == '__main__':
    t = testclass("dd", "ee", "gg")
问题:
如果我有几个属性而不是三个,那么编写
meth
方法的最佳方法是什么?我将对所有属性执行相同的检查。

最简单的方法是不要将它们存储为单个属性。在本例中,我使用参数打包将
a
b
c
作为元组
abc
,并将其存储为
self.\u attrs

class testclass(object):
    def __init__(self, *abc):
        self._attrs = abc

    def meth(self):
        for attr in self._attrs:
            print attr

if __name__ == '__main__':
    t = testclass("dd", "ee", "gg")

不太清楚你所说的检查属性是什么意思

但我仍然认为

def获取(自身、实例、所有者):


还有,如果你想访问实例的所有属性,你可以调用
dir(self)
,但是你也会得到内置的。最好抓取你想要的东西,妥善储存