在Python中,类经过 class 关键字定义。以 Person 为例,定义一个Person类以下:python
class Person(object):
pass
按照 Python 的编程习惯,类名以大写字母开头,紧接着是(object),表示该类是从哪一个类继承下来的。类的继承将在后面的章节讲解,如今咱们只须要简单地从object类继承。编程
有了Person类的定义,就能够建立出具体的xiaoming、xiaohong等实例。建立实例使用 类名+(),相似函数调用的形式建立:python2.7
xiaoming = Person()
xiaohong = Person()
请建立包含两个 Person 类的实例的 list,并给两个实例的 name 赋值,而后按照 name 进行排序。对象
1 #python 2.7 2 class Person(object): 3 pass 4 5 p1 =Person() 6 p1.name ='Bart' 7 p2 =Person() 8 p2.name ='Adam' 9 p3 =Person() 10 p3.name ='Lisa' 11 L1 =[p1, p2, p3] 12 L2 = sorted(L1,key=lambda x: x.name) 13 print L2[0].name 14 print L2[1].name 15 print L2[2].name
输出blog
Adam
Bart
Lisa排序
请定义Person类的__init__方法,除了接受 name、gender 和 birth 外,还可接受任意关键字参数,并把他们都做为属性赋值给实例。
1 #python2.7 2 class Person(object): 3 def __init__(self,name,gender,birth,**kw): 4 self.name = name 5 self.gender = gender 6 for k,w in kw.items(): 7 setattr(self,k,w) 8 9 xiaoming =Person('xiao ming','Male','1990-1-1', job='Student') 10 print xiaoming.name 11 print xiaoming.job
输出
xiao ming
Student
请给Person类的__init__方法中添加name和score参数,并把score绑定到__score属性上,看看外部是否能访问到。
1 #python2.7 2 class Person(object): 3 def __init__(self, name, score): 4 self.name = name 5 self.__score = score 6 def __str__(self): 7 return str(self.__score) 8 9 p =Person('Bob',59) 10 print p.name 11 print p 12 print p.__score
输出
Bob
59
Traceback (most recent call last):
File "test.py", line 12, in <module>
print p.__score
AttributeError: 'Person' object has no attribute '__score'
请给 Person 类添加一个类属性 count,每建立一个实例,count 属性就加 1,这样就能够统计出一共建立了多少个 Person 的实例。
1 #python 2.7 2 class Person(object): 3 count =0 4 def __init__(self,name): 5 self.name = name 6 7 p1 =Person('Bob') 8 Person.count +=1 9 print Person.count 10 p2 =Person('Alice') 11 Person.count +=1 12 print Person.count 13 p3 =Person('Tim') 14 Person.count +=1 15 print Person.count
输出
1
2
3
请把上节的 Person 类属性 count 改成 __count,再试试可否从实例和类访问该属性。
1 class Person(object): 2 __count =0 3 def __init__(self, name): 4 Person.__count +=1 5 self.name = name 6 7 p1 =Person('Bob') 8 p2 =Person('Alice') 9 print Person.__count
输出
Traceback (most recent call last):
File "test.py", line 9, in <module>
print Person.__count
AttributeError: type object 'Person' has no attribute '__count'
请给 Person 类增长一个私有属性 __score,表示分数,再增长一个实例方法 get_grade(),能根据 __score 的值分别返回 A-优秀, B-及格, C-不及格三档。
1 #python 2.7 2 class Person(object): 3 def __init__(self, name, score): 4 self.name = name 5 self.__score = score 6 def get_grade(self): 7 if self.__score >=85: 8 return'A' 9 elif self.__score >=60: 10 return'B' 11 else: 12 return'C' 13 14 p1 =Person('Bob',90) 15 p2 =Person('Alice',65) 16 p3 =Person('Tim',48) 17 print p1.get_grade() 18 print p2.get_grade() 19 print p3.get_grade()
输出
A
B
C
因为属性能够是普通的值对象,如 str,int 等,也能够是方法,还能够是函数,你们看看下面代码的运行结果,请想想 p1.get_grade 为何是函数而不是方法:
class Person(object): def __init__(self, name, score): self.name = name self.score = score self.get_grade = lambda: 'A' p1 = Person('Bob', 90) print p1.get_grade print p1.get_grade()
结果
<function <lambda> at 0x029F9AF0> A
4.10 python中定义类方法
若是将类属性 count 改成私有属性__count,则外部没法读取__score,但能够经过一个类方法获取,请编写类方法得到__count值。
1 #python 2.7 2 class Person(object): 3 __count =0 4 @classmethod 5 def how_many(cls): 6 return cls.__count 7 def __init__(self,name): 8 self.name = name 9 Person.__count +=1 10 11 print Person.how_many() 12 p1 =Person('Bob') 13 print Person.how_many()
输出
01