廖雪峰Python学习笔记day6

学习笔记day5

# 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创建出来的“实例”

在这里插入图片描述
学习笔记day7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值