本文主要讲述python面向对象的一些基础语法。python
建立一个名为Person类,继承自object类(object类是全部类的祖先类),类体为空:json
class Person(object): pass
建立一个Person类的实例:ssh
p1 = Person()
为p1动态添加一个'name'属性:函数
p1.name = 'Tom' print p1.name
Tom
为p1动态添加方法(其实方法也能够当作是对象实例的特殊属性):code
import types p1.get_name = types.MethodType(lambda self:self.name,p1,Person) print p1.get_name()
Tom
再建立另一个Person对象:orm
p2 = Person() print p2.name
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-10-e3abcd8b0fdd> in <module>() 1 p2 = Person() ----> 2 print p2.name AttributeError: 'Person' object has no attribute 'name'
能够看出为p1对象动态添加的'name'属性并不会影响到Person类的其余对象。对象
class Person(object): # 这里self为实例方法的第一个必须的参数,必需要有,且要放在第一个;第二个name为自定义必选参数,后面的kwargs是可选参数列表 def __init__(self,name,**kwargs): self.name = name for k,v in kwargs.iteritems(): # 设置实例属性值 setattr(self,k,v)
p = Person('Tom',age = 19,gender = 'male') print p.name print p.age print p.gender
Tom 19 male
python中实例的属性(包括属性和方法)是根据命名来进行可见性约束的,规则以下:继承
attr
内外部均可见ip
__attr__
预置属性,内外部均可见,但不建议普通属性这样命名开发
_attr
内部可见、外部不可见(但只是倡议,并不是强制约束)
__attr
内部可见、外部不可见(强制约束)
举例说明:
class Person(object): attr1 = 1 __attr2__ = 2 _attr3 = 3 __attr4 = 4
p = Person()
print p.attr1
1
print p.__attr2__
2
print p._attr3
3
print p.__attr4
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-20-47493f4f761b> in <module>() ----> 1 print p.__attr4 AttributeError: 'Person' object has no attribute '__attr4'
写在类中,而非方法中的属性,其可见性规则和实例属性相似。
注:当实例修改了某个类属性以后,实际上是建立了一个新的同名的实例属性,并不会让类属性值自己真正发生改变。而实例属性的访问优先级是高于类属性的。
类方法就是用@classmethod
装饰器修饰的方法。`
这是全部类的祖先类。一个自定义类若是不指定继承哪一个类,那它默认继承object类,如:
class Person: pass p = Person() print isinstance(p,Person) print isinstance(p,object)
True True
def __init__(self,arg1,arg2,...): ...
在子类的构造方法中要调用父类的构造方法进行初始化,才能获取到父类的实例属性(获取类属性不须要),调用方式:
# SubClass:子类类名 super(SubClass,self).__init__(attr1,attr2,...) #这里不用再写'self'参数了
举例:
class Person(object): attr = 1 def __init__(self,name,age): self.name = name self.age = age class Student(Person): def __init__(self,name,age,score): super(Student,self).__init__(name,age) self.score = score
s = Student('Tom',18,98)
print s
<__main__.Student object at 0x03A31D10>
print s.age print s.name print s.score print s.attr
18 Tom 98 1
小贴士:python做为动态语言,调用实例的方法时,并不会去检查类型的合法性,只要调的方法是实例有的而且参数正确,就能够调用。
例如:只要一个对象实例有名为'read'的方法,它就是一个file-like对象,就能够做为参数传入json.load()函数中。
class A: pass class B(A): pass class C(A): pass class D(B,C): pass d = D() print isinstance(d,D) print isinstance(d,B) print isinstance(d,C) print isinstance(d,A)
True True True True
能够看出B、C类都继承了A类,而D类又同时继承了B、C类,因此D类的对象同时也是A、B、C三个类的实例。
class Person(object): name = 'Tom' p = Person()
print isinstance(p,Person)
True
print isinstance([1,2,3],list)
True
print type(p)
<class '__main__.Person'>
print dir(p)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']
print getattr(p,'name')
Tom
setattr(p,'age',19) print p.age
19
__str__(self)
和__repr__(self)
:用于print等显示函数,前者用于给用户看,后者给开发看。
__cmp__(self,s)
:用于cmp()
、sorted()
等和顺序有关的函数,其中s为另外一个同类型的对象。
__len__(self)
:用于len()
函数。
__add__(self,s)
、__sub__(self,s)
、__mul__(self,s)
、__div__(self,s)
:用于对象的加减乘除四则运算。
__int__(self)
:用于int()
函数。
__float__(self)
:用于float()
函数。
__slots__(self)
:返回一个字符串列表或元组,限制类能够具备的属性。
__call__(self)
:让对象变成可调用的(用双括号()
运算符调用)。
注:实现了__call__(self)
方法的对象就变成了能够像函数那样调用的了,因此python的对象和函数的区分其实不明显。
举例:
class Say(object): def __call__(self,a,b): print 'You say:{0} and {1}'.format(a,b) s = Say() s('hello','world')
You say:hello and world
@property
装饰器:装饰属性的getter方法。
@attr.setter
装饰器:装饰属性的setter方法。
举例:
class Student(object): def __init__(self,name,score): self.name = name # 这里score定义成一个私有的属性 self.__score = score # 至关于getter方法 @property def score(self): return self.__score # 至关于setter方法 @score.setter def score(self,score): if score > 100 or score < 0: raise ValueError('Invalid score!') self.__score = score # 定义一个新的grade属性 @property def grade(self): if self.__score >= 80: return 'A' elif self.__score < 60: return 'C' else: return 'B' s = Student('Tom',90) print s.grade s.score = 59 print s.grade print s.score
A C 59
s.score = -1
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-10-7a8b6a159a64> in <module>() ----> 1 s.score = -1 <ipython-input-9-9b3f1ca25afb> in score(self, score) 14 def score(self,score): 15 if score > 100 or score < 0: ---> 16 raise ValueError('Invalid score!') 17 self.__score = score 18 ValueError: Invalid score!
s.grade = 'C'
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-11-de53788b008a> in <module>() ----> 1 s.grade = 'C' AttributeError: can't set attribute