目录:
1、静态方法 @staticmethod
2、类方法 @classmethod
3、属性方法 @property
4、类的特殊成员方法
a) __doc__表示类的描述信息
b) __module__和 __class__
c) __call__对象后面加括号,触发执行
d) __dict__查看类或者对象中的所有成员
e) __str__如果一个类中定义了__str__方法,那么打印对对象时,默认输出该方法的返回值
f)__getitem__ __setitem__ __delitem__
g) __new__ __metaclass__
5、反射
6、异常处理
7、socket编程
一、基础概念
1、@staticmethod装饰器
即可把装饰的方法变成一个静态方法,在类中,普通的类方法可以通过实例化后直接调用,并且在方法里可以通过self调用实例变量或者类变量,但是静态方法是不可以访问实例变量或者类变量的,一个不能方法实例变量和类变量的方法,其实相当于已经跟类本身脱离了联系,它与类唯一的关联就是需要通过类名来调用这个方法
class Dog(object): def __init__(self,name): self.name = name @staticmethod #当staticmethod装饰器做装饰时,eat方法已经和Dog类脱离 def eat(self,food): print('%s is eating %s' % (self.name, food)) d = Dog('Tom') d.eat('包子') #通过普通的实例化后调用是没法调用静态方法装饰的类方法的 #输出 Traceback (most recent call last): File "/Users/Gavin/PycharmProjects/python/day7/复现/1-静态方法.py", line 11, in <module> d.eat('包子') #通过普通的实例化后调用是没法调用静态方法装饰的类方法的 TypeError: eat() missing 1 required positional argument: 'food'
静态方法调用可以有两种方式:
1、调用时主动传递实例本身给eat方法,即d.eat(d)
class Dog(object): def __init__(self,name): self.name = name @staticmethod #当staticmethod装饰器做装饰时,eat方法已经和Dog类脱离 def eat(self,food): print('%s is eating %s' % (self.name, food)) d = Dog('Tom') d.eat(d,'包子') #输出 Tom is eating 包子
2、在eat方法中去掉self参数,但这也意味着,在eat中不能通过self调用实例中其他实例变量了
class Dog(object): def __init__(self,name): self.name = name @staticmethod #当staticmethod装饰器做装饰时,eat方法已经和Dog类脱离 def eat(): print('is eating ') d = Dog('Tom') d.eat() #输出 is eating
2、类方法
类方法通过@classmethod装饰器实现,类方法和普通方法区别在于,类方法只能访问类变量,而不能访问实例变量
class Dog(object): def __init__(self,name): self.name = name @classmethod def eat(self,food): print('%s is eating %s' % (self.name,food)) d = Dog('Tom') d.eat('包子')
报错如下:说Dog类中没有name这个属性,因为name是实例变量,类方法是不能访问实例变量的
Traceback (most recent call last): File "/Users/Gavin/PycharmProjects/python/day7/复现/2-类方法.py", line 12, in <module> d.eat('包子') File "/Users/Gavin/PycharmProjects/python/day7/复现/2-类方法.py", line 8, in eat print('%s is eating %s' % (self.name,food)) AttributeError: type object 'Dog' has no attribute 'name'
此时定义一个类变量,也叫name,这样就可以访问了
class Dog(object): name = 'Jarry' def __init__(self,name): self.name = name @classmethod def eat(self,food): print('%s is eating %s' % (self.name,food)) d = Dog('Tom') d.eat('包子') #输出 Jarry is eating 包子
3、属性方法
属性方法的作用就是通过@property把一个方法变成静态属性
class Dog(object): def __init__(self,name): self.name = name @property def eat(self): print('%s is eating ' %self.name) d = Dog('Tom') d.eat #属性方法将Dog类中的方法变为属性(变量),在调用时不需要加(),按照属性的引用方法直接引用 #输出 Tom is eating
对属性方法的属性添加
class Dog(object): def __init__(self,name): self.name = name self.__food = None #添加私有属性 @property def eat(self): print('%s is eating %s' % (self.name,self.__food)) @eat.setter #对属性方法的属性添加,需要通过 方法名.setter方式 def eat(self,food): print('set the food', food) self.__food = food #可以通过方式进行私有属性的添加 d = Dog('Tom') d.eat #输出 Tom is eating None d.eat = '包子' #对属性方法的属性添加可以采用该方式完成 #输出 set the food 包子 d.eat #输出 Tom is eating 包子
删除属性方法中所携带的属性:属性方法的属性没法直接删除
class Dog(object): def __init__(self,name): self.name = name self.__food = None #添加私有属性 @property def eat(self): print('%s is eating %s' % (self.name,self.__food)) #@eat.deleter #def eat(self): # del self.__food # print('删除self.__food属性') d = Dog('Tom') d.eat del d.eat print(d.eat)
当对d.eat属性方法删除属性时,会报不能删除属性的错误
Traceback (most recent call last): Tom is eating None File "/Users/Gavin/PycharmProjects/python/day7/复现/3-属性方法/属性方法-删除属性.py", line 27, in <module> del d.eat AttributeError: can't delete attribute
如果需要删除属性方法所对应属性时,需要通过@方法名.deleter方法来删除
class Dog(object): def __init__(self,name): self.name = name self.__food = None #添加私有属性 @property def eat(self): print('%s is eating %s' % (self.name,self.__food)) @eat.deleter def eat(self): del self.__food print('删除self.__food属性') d = Dog('Tom') d.eat del d.eat print(d.eat) #输出 Traceback (most recent call last): Tom is eating None File "/Users/Gavin/PycharmProjects/python/day7/复现/3-属性方法/属性方法-删除属性.py", line 23, in <module> print(d.eat) File "/Users/Gavin/PycharmProjects/python/day7/复现/3-属性方法/属性方法-删除属性.py", line 11, in eat 删除self.__food属性 print('%s is eating %s' % (self.name,self.__food)) AttributeError: 'Dog' object has no attribute '_Dog__food' #该句表明已删除Dog类实例化后中的d.eat属性
属性方法用途:
在OOP编程中的许多场景中不能简单通过定义静态属性来实现,需要根据不同的场景(这些场景很多时候是自己不能控制的)来返回不同的值
举例说明:
制作一个航班APP,这个APP会报告航班当前的状态,这个状态分为:到达、延迟、取消、还是飞走了,想知道这种状态必须经历以下步骤:
1、链接航空公司的API查询
2、对查询结果进行解析
3、返回结果给APP的用户
因此这个status属性的值是一系列动作后得到的结果,所以每次调用时,其实它都要经过一系列的动作才返回结果,但这些动作过程不需要用户关心,用户只需要调用这个属性就可以
航班查询:
class Flight(object): def __init__(self,flight_name): self.flight_name = flight_name def checking_status(self): print("checking flight %s status " % self.flight_name) return 1 @property def flight_status(self): status = self.checking_status() if status == 1: print('the flight %s has arrived!' % self.flight_name) elif status == 0: print('the flight %s is cancelled!' % self.flight_name) elif status == 2: print('the flight %s is flighting!' % self.flight_name) f = Flight('CA2543') f.flight_status #输出 checking flight CA2543 status the flight CA2543 has arrived!
修改与删除属性方法
class Flight(object): def __init__(self,flight_name): self.flight_name = flight_name self.__status = 0 def checking_status(self): #print("checking flight %s status " % self.flight_name) return 1 @property def flight_status(self): if self.__status == 1: print('the flight %s has arrived!' % self.flight_name) elif self.__status == 0: print('the flight %s is cancelled!' % self.flight_name) elif self.__status == 2: print('the flight %s is flighting!' % self.flight_name) @flight_status.setter def flight_status(self,status): #修改属性方法 self.__status = status @flight_status.deleter def flight_status(self): #删除属性方法 print("status got removed...") del self.__status f = Flight('CA2543') f.flight_status #输出 the flight CA2543 is cancelled!
f.flight_status = 2 f.flight_status #输出 the flight CA2543 is flighting!
del f.flight_status #输出 status got removed...
f.flight_status #输出 File "/Users/Gavin/PycharmProjects/python/day7/复现/3-属性方法/航班查询.py", line 37, in <module> f.flight_status File "/Users/Gavin/PycharmProjects/python/day7/复现/3-属性方法/航班查询.py", line 13, in flight_status if self.__status == 1: AttributeError: 'Flight' object has no attribute '_Flight__status'
4、类的特殊成员
a)__doc__ 类的描述信息
class Flight(object): '''该类用于航班状态查询''' def __init__(self,flight_name): self.flight_name = flight_name self.__status = 0 def checking_status(self): #print("checking flight %s status " % self.flight_name) return 1 @property def flight_status(self): if self.__status == 1: print('the flight %s has arrived!' % self.flight_name) elif self.__status == 0: print('the flight %s is cancelled!' % self.flight_name) elif self.__status == 2: print('the flight %s is flighting!' % self.flight_name) @flight_status.setter def flight_status(self,status): #修改属性方法 self.__status = status @flight_status.deleter def flight_status(self): #删除属性方法 print("status got removed...") del self.__status f = Flight('CA2843') print(f.__doc__) #打印类的描述信息 #输出 该类用于航班状态查询