# python study day6
# 类属性
# class Student(object):
# name = 'Student'
# s = Student()
# print(s.name) #>>> Student
# print(Student.name) #>>> Student
# s.name = 'newName'
# print(s.name) #>>> newName
# del s.name
# print(s.name) #>>> Student
# 计算实例数量
# class Student(object):
# count = 0
# def __init__(self, name):
# self.name = name
# Student.count +=1
# 实例动态绑定属性
# s = Student()
# s.name = 'newName'
# 实例动态绑定方法
# def set_age(self, age):
# self.age = age
# from types import MethodType
# s.set_age = MEthodType(set_age, s)
# 给类class动态方法
# Student.set_age = set_age
# 使用__slots__限制动态绑定属性
# class Student(Object):
# __slots__ = ('name', 'age') # 用tuple定义允许绑定的属性名称
# s.socre = 99 # slots没有score >>> AttributeError
# 子类未定义slots时不受父类影响,定义后限制是子类+父类
# 内置 @property 可以把一个方法变成属性调用
# class Student(object):
# @property
# def score(self):
# return self.score
# @score.setter
# def sore(self, value):
# if not isinstance(value, int):
# raise ValueError('score must be an Integer!')
# if value < 0 or value > 100:
# raise ValueError('score must betweeen 0~100!')
# s = Student()
# s.score = 60 # 实际转化为s.set_score(60)
# print(s.score) # 实际转化为s.get_score()
# 也可以不添加 @score.setter 注解,该属性就是只读属性
# class Screen(object):
# @property
# def width(self):
# return self._width
# @width.setter
# def width(self, width):
# self._width = width
# @property
# def height(self):
# return self._height
# @height.setter
# def height(self, height):
# self._height = height
# @property
# def resolution(self):
# return self._width*self._height
# s = Screen()
# s.width = 1024
# s.height = 768
# print('resolution =', s.resolution)
# Python 允许多重继承,类似java接口实现
# class Dog(Animal, Runnable):
# pass
# 为看出继承关系可以引入MixIn
# class MyTCPServer(TCPServer, ForkingMixIn):
# pass # 多进程模式TCP服务
# class MyUDPServer(UDPServer, ThreadingMixIn):
# pass # 多线程模式UDP服务
# 定制类
# class Student(object):
# pass
# print(Student()) # 调用__str__()方法,打印实例地址
# >Student() # 调用__repr__()方法,打印实例地址
# class Student(object): # 改写打印内容
# def __init__(self, name):
# self.name = name
# def __str__(self):
# return 'Student object(name=%s)' % self.name
# __repr__ == __str__
# __iter__() 方法 list、tuple 类型的 for…in 循环实现
# __next__() 方法拿到循环下一个值
# __getitem__() 方法实现list下标获取值,切片,step步长参数,key等
# __setitem__() 方法对list、dict等集合赋值
# __delitem__() 方法用于删除元素
# __getattr__() 方法,在对象调用查找不到属性时调用。如:链式调用
# class Chain(object):
# def __init__(self, path=''):
# self._path = path
# def __getattr__(self, path):
# return Chain('%s/%s' % (self._path, path))
# def __str__(self):
# return self._path
# __repr__ = __str__
# print(Chain().status.user.itemline.list) #>>>/status/user/itemline/list
# __call__() 方法,定义后可以直接对实例进行调用,执行该方法
# class Student(object):
# def __init__(self, name):
# self._name = name
# def __call__(self):
# print('My name is %s.' % self._name)
# s = Student('Straw')
# s() #>>> My name is Straw.
# # 可以通过callable()函数判断类实例是否能被调用
# callable(max) #>>> True
# callable('str') #>>> False
# 枚举类,python提供了Enum类
# from enum import Enum
# Month = Enum('Month', ('Jan','Feb','Mar','Apr','May',
# 'Jun','Jul','Aug','Sep','Oct','Nov','Dec'))
# 从Enum派生出自定义类
# from enum import Enum, unique
# @unique # 可以自动检查是否有重复值
# class Weekday(Enum):
# Sun = 0
# Mon = 1
# Tue = 2
# Wed = 3
# Thu = 4
# Fri = 5
# Sat = 6
# print(Weekday.Mon) #>>> Weekday.Mon
# print(Weekday['Tue']) #>>> weekday.Tue
# print(Weekday.Tue.value) #>>> 2
# print(Weekday(1)) #>>> Weekday.Mon
# for name, per in Weekday.__members__.items():
# print(name, '==>', per)
# from enum import Enum
# class Gender(Enum):
# Male = 0
# Female = 1
# gender = Gender.Male
# print(isinstance(gender, Gender))
# class Student(object):
# def __init__(self, name, gender):
# self.name = name
# if isinstance(gender, Gender):
# self.gender = gender
# else:
# raise ValueError('gender value must be Gender Enum!')
# bart = Student('Bart', 0)
# type() 函数既可以返回一个对象的类型,也可以动态创建一个类
# def fn(self, name='world'): # 先定义函数
# print('Hello, %s' % name)
# Hello = type('Hello', (object,), dict(hello=fn))
# # 创建Hello class。完成三个参数,1.类名 2.继承的父类集合 3.class方法名函数绑定
# h = Hello()
# h.hello() #>>> Hello, world.
# print(type(Hello)) #>>> <class 'type'>
# print(type(h)) #>>> <class '__main__.Hello'>
# 元类 metaclass。一般实例构造过程:先定义metaclass、创建类、最后创建实例
# metaclass 允许创建类或者修改类,或者可以将类看成由metaclassc创建出来的“实例”
廖雪峰Python学习笔记day6
最新推荐文章于 2024-02-17 03:00:00 发布