1.__slots__
通过Types包中的MethodType将外部方法与类对象进行绑定(该方法只能对绑定的对象生效)
"""a test module"""
from types importMethodType__author__ = 'Jack Ma'
classStudent(object):"""a test class"""
pass
defset_name(self, name):
self.name=name;
a=Student()
a.set_name=MethodType(set_name, a, Student)
a.set_name("Jack")print a.name
通过Types包中的MethodType将外部方法与类进行绑定(该方法能够被该类的所有对象使用)
"""a test module"""
from types importMethodType__author__ = 'Jack Ma'
classStudent(object):"""a test class"""
pass
defset_name(self, name):
self.name=name;
a=Student()
b=Student()
Student.set_name=MethodType(set_name, None, Student)
a.set_name("Jack")
b.set_name("Tom")printa.nameprint b.name
而当我们想对一个类可以赋予的属性进行限制,用到类的__slots__参数即可,__slots__赋值使用字符串为成员的元组进行赋值
classStudent(object):"""a test class"""
__slots__ = ('name', 'age')pass
defset_name(self, name):
self.name=name;
a=Student()
Student.set_name=MethodType(set_name, None, Student)
a.set_name("Jack")
a.sex= "Male"
print a.name, a.sex
结果是sex属性插入不了,报错
Traceback (most recent call last):
File"F:/PyProject/test2.py", line 23, in a.sex= "Male"AttributeError:'Student' object has no attribute 'sex'
但是方法可以插入到类中
注意:使用__slots__要注意,__slots__定义的属性仅对当前类起作用,对继承的子类是不起作用的。除非在子类中也定义__slots__,这样,子类允许定义的属性就是自身的__slots__加上父类的__slots__。
from types importMethodType__author__ = 'Jack Ma'
classStudent(object):"""a test class"""
__slots__ = ('name', 'age')pass
classClass1(Student):"""a test son Class"""
__slots__ =()defset_name(self, name):
self.name=name;
a=Class1()
Class1.set_name=MethodType(set_name, None, Class1)
a.set_name("Jack")
a.sex= "Male"
print a.name, a.sex
此时,父类的__slots__限制才能生效
2.使用@property
classStudent(object):"""a test class"""@propertydefscore(self):returnself._score
@score.setterdefscore(self, value):if notisinstance(value, int):raise ValueError('score must be an integer!')if value < 0 or value > 100:raise ValueError('score must between 0 ~ 100!')
self._score=value
a=Student()
a.score= 11
print a.score
可以通过装饰器的setter属性 对score的设置属性进行限制
3.多继承
需要多继承时,在第二个类及之后的类后加上MiXin实现
classMyTCPServer(TCPServer, CoroutineMixin):pass
4.元类
用type()函数创建类
3个参数:
1.创建的类名 -- 字符串
2.父类 -- 元组
3.绑定的内容 -- 字典
deffunc(self, name):print "Hello %s !" %name
Hello= type("Hello", (object,), {'hello': func})
a=Hello()
a.hello("World")
Hello World !
除了使用type()动态创建类以外,要控制类的创建行为,还可以使用metaclass。
metaclass,直译为元类,简单的解释就是:
当我们定义了类以后,就可以根据这个类创建出实例,所以:先定义类,然后创建实例。
但是如果我们想创建出类呢?那就必须根据metaclass创建出类,所以:先定义metaclass,然后创建类。
连接起来就是:先定义metaclass,就可以创建类,最后创建实例。
所以,metaclass允许你创建类或者修改类。换句话说,你可以把类看成是metaclass创建出来的“实例”。
本文详细介绍了Python中类的使用,包括__slots__特性来限制对象属性,MethodType用于绑定方法,以及@property装饰器进行属性限制。同时讨论了多继承中的Mixin用法和元类的概念,展示了如何通过type创建类以及元类在控制类创建中的作用。
1462

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



