当名称相同时,python如何区分类属性,实例属性和方法?
class Exam(object):
test = "class var"
def __init__(self, n):
self.test = n
def test(self):
print "method : ",self.test
test_o = Exam("Fine")
print dir(test_o)
print Exam.test
print test_o.test
test_o.test()
输出:
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'test']
Fine
Traceback (most recent call last):
File "example.py", line 32, in
test_o.test()
TypeError: 'str' object is not callable
如何调用
> class属性,Exam.test – > < unbound method load.test>输出显示方法
>实例属性test_o.test – > “精细”
> method test_o.test() – > TypeError:’str’对象不可调用