学习资源来源:慕课网Python进阶
类
class Person(object):pass
初始化实例属性:
lass Person(object):
def __init__(self, name, gender, birth):
self.name = name
self.gender = gender
self.birth = birth__init__() 方法的第一个参数必须是 self,创建实例时,就必须要提供除 self 以外的参数
xiaoming = Person('Xiao Ming', 'Male', '1991-1-1')
接收任意关键字参数作为属性
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')使用**kw接收任意任意参数
访问限制
以双下划线开头__,该属性就无法被外部访问。
以"__xxx__"定义的属性在Python的类中被称为特殊属性,可以被外部访问,有很多预定义的特殊属性可以使用。
以单下划线开头的属性"_xxx"虽然也可以被外部访问,但是,按照习惯,他们不应该被外部访问。
类属性
实例属性每个实例各自拥有,互相独立,而类属性有且只有一份。
class Person(object):
address = 'Earth'
def __init__(self, name):
self.name = name
当实例属性和类属性重名时,实例属性优先级高,它将屏蔽掉对类属性的访问。
p1 = Person('Bob')
p2 = Person('Alice')
p1.address = 'China'
print p1.address+' '+Person.address+' '+p2.address得到China Earth Earth
类方法
方法也分实例方法和类方法,在class中定义的全部是实例方法,实例方法第一个参数 self 是实例本身。
类方法通过标记一个 @classmethod,该方法将绑定到 Person 类上,而非类的实例。类方法的第一个参数将传入类本身,通常将参数名命名为 cls。
class Person(object):
__count = 0
@classmethod
def how_many(cls):
return cls.__count
def __init__(self, name):
self.name = name
Person.__count = Person.__count + 1
继承类
用 super(SubClass, self).__init__(args) 去初始化父类,函数super(Student, self)将返回当前类继承的父类,即 Person ,然后调用__init__()方法。
注意self参数已在super()中传入,在__init__()中将隐式传递,不需要写出(也不能写)。
class Student(Person):
def __init__(self, name, gender, score):
super(Student, self).__init__(name, gender)
self.score = score
多态
方法调用将作用在实际类型上。调用方法时总是先查找它自身的定义,如果没有定义,则顺着继承链向上查找,直到在某个父类中找到为止。
多重继承
除了从一个父类继承外,Python允许从多个父类继承,称为多重继承。
获取对象信息
用 isinstance() 判断它是否是某种类型的实例。
用 type() 函数获取变量的类型,它返回一个 Type 对象。
特殊方法
Python 定义了__str__()和__repr__()两种方法,__str__()用于显示给用户,而__repr__()用于显示给开发人员。class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender
def __str__(self):
return '(Person: %s, %s)' % (self.name, self.gender)
p = Person('Bob', 'male')
print p
p
得到(Person: Bob, male) <main.Person object at 0x10c941890>
Python用__cmp__()用实例自身self和传入的实例 s 进行比较,如果 self 应该排在前面,就返回 -1,如果 s 应该排在前面,就返回1,如果两者相当,返回 0。
Python用__len__()返回元素的个数。
Python用__call__()实现讲一个类变成可调用函数class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender
def __call__(self, friend):
print 'My name is %s...' % self.name
print 'My friend is %s...' % friend
p = Person('Bob', 'male')
p('Tim')
得到
My name is Bob...
My friend is Tim..
Python用__slots__限制添加属性,目的是限制当前类所能拥有的属性,如果不需要添加任意动态的属性,使用__slots__也能节省内存。
class Student(object):
__slots__ = ('name', 'gender', 'score')此后不能动态添加属性。
@property:
class Student(object):
def __init__(self, name, score):
self.name = name
self.__score = score
@property
def score(self):
return self.__score
@score.setter
def score(self, score):
if score < 0 or score > 100:
raise ValueError('invalid score')
self.__score = score第一个score(self)是get方法,用@property装饰,第二个score(self, score)是set方法,用@score.setter装饰,@score.setter是前一个@property装饰后的副产品。
1486

被折叠的 条评论
为什么被折叠?



