1、python中初始化实例属性
classmethod, staticmethod, self
_arr, _internal. __private_method,__private_attr
| 1 | 必须在__init__(self,…)方法内(注意:双下划线)初始化实例,第一个参数必须为self。 |
| 1 2 3 4 5 6 7 8 9 10 | class Person(object): def __init__(self,name,gender,birth,**kw): self.name = name self.gender = gender self.birth = birth for k,v in kw.iteritems() : setattr(self,k,v) xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student') print xiaoming.name print xiaoming.job |
2、python中访问限制
Python对属性权限的控制是通过属性名来实现的,如果一个属性由双下划线开头(__),该属性就无法被外部访问。
如果一个属性以"__xxx__"的形式定义,那它又可以被外部访问了,以"__xxx__"定义的属性在Python的类中被称为特殊属性,有很多预定义的特殊属性可以使用,通常我们不要把普通属性用"__xxx__"定义。
以单下划线开头的属性"_xxx"虽然也可以被外部访问,但是,按照习惯,他们不应该被外部访问。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class Person(object): def __init__(self, name): self.name = name self._title = 'Mr' self.__job = 'Student' p = Person('Bob') print p.name # => Bob print p._title # => Mr print p.__job # => Error Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'Person' object has no attribute '__job' |
3、python中创建类属性
实例属性每个实例各自拥有,互相独立,而类属性有且只有一份。
| 1 2 3 4 | class Person(object): address = 'Earth' def __init__(self, name): self.name = name |
调用 Person.address 即可获得类属性,通过实例对象也可以获得类属性
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Person(object): count = 0 def __init__(self,name='Smith'): Person.count = Person.count + 1 self.name = name p1 = Person('Bob') print Person.count p2 = Person('Alice') print Person.count p3 = Person('Tim') print Person.count p4 = Person() |
| 1 2 | p5 = Person() print Person.count |
| 1 | 注意,python不支持构造函数的重载。但通过设置默认值,调用构造函数时就可以省略参数。 |
4、python中类属性和实例属性名字冲突怎么办
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class Person(object): address = 'Earth' def __init__(self, name): self.name = name p1 = Person('Bob') p2 = Person('Alice') print 'Person.address = ' + Person.address p1.address = 'China' print 'p1.address = ' + p1.address print 'Person.address = ' + Person.address print 'p2.address = ' + p2.address |
| 1 | 当实例属性和类属性重名时,实例属性优先级高,它将屏蔽掉对类属性的访问。 |
| 1 | <strong>类的属性也有访问权限,带双下划线(__xxx)的属性无法从外部访问。</strong> |
5、python中定义实例方法
类实例方法的定义在类体中,第一个参数必须是self,调用时无需显示传入。
| 1 2 3 4 5 6 7 8 9 | class Person(object): def __init__(self, name): self.__name = name def get_name(self): return self.__name a = Person("Jim") a.get_name() |
6、python中方法也是属性
| 1 | 可以动态的把方法绑定到一个对象上去(虽然并不常用): |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import types def fn_get_grade(self): if self.score >= 80: return 'A' if self.score >= 60: return 'B' return 'C' class Person(object): def __init__(self, name, score): self.name = name self.score = score p1 = Person('Bob', 90) p1.get_grade = types.MethodType(fn_get_grade, p1, Person) print p1.get_grade() # => A p2 = Person('Alice', 65) print p2.get_grade() # ERROR: AttributeError: 'Person' object has no attribute 'get_grade' # 因为p2实例并没有绑定get_grade |
7、python中定义类方法
| 1 | 通过@classmethod语句将方法绑定到Person类上。 |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | class Person(object): __count = 0 @classmethod def how_many(cls): return cls.__count def __init__(self,name): Person.__count += 1 self.name = name print Person.how_many() p1 = Person('Bob') print Person.how_many() |
参考文献:http://www.imooc.com/code/6172