python -- getattr , __dict__, startwith
上例子
class A(object):
a = 0
b = 1
def __init__(self):
self.a = 2
self.b = 3
def test(self):
print( 'a normal func.')
@staticmethod
def static_test(self):
print ('a static func.')
@classmethod
def class_test(self):
print ('a calss func.')
obj = A()
print( A.__dict__)
print (obj.__dict__)
a = {k: getattr(A, k) for k, _ in A.__dict__.items() \
if not k.startswith('_')}
print(a)
- Python的getattr()函数返回对象的对应属性值
class A(object):
def __init__(self):
self.a = 2
self.b = 3
obj = A()
a = getattr(obj,'a')
print(a)
2
- python 的startwith方法,检查字符是否以指定字符串开头
- python __dict__属性,返回类中的一些属性
由此可见, 类的静态函数、类函数、普通函数、全局变量以及一些内置的属性都是放在类__dict__里的