python中类属性与实例属性总结
stackoverflow上面的相关讨论
http://stackoverflow.com/questions/2923579/python-class-attribute
http://stackoverflow.com/questions/1944625/what-is-the-relationship-between-getattr-and-getattr
1. 类属性
为在类定义时直接指定的属性(不是在__init__方法中)
import random#I think this is a good example to show the power you can get from overriding '__getattribute__'class TestClass(object):def __init__(self,stu1,*args): #at least one student!self.students=[stu1]self.students.extend(args) def __getattribute__(self,attr_name):if attr_name=="random_student":students=super(TestClass,self).__getattribute__("students")pos=random.randint(0,len(students)-1)return students[pos]else:return super(TestClass,self).__getattribute__(attr_name)