__slots__ restricting instances to add extra attributes
To achieve the limit, Python allows you to define a special __slots__ variable to limit the attributes that the class instance can add when defining class:
class Student (object): __slots__ = ('name','age') #
Use __slots__ to note that __slots__-defined properties do not work on inherited subclasses only for the current class instance
class graduatestudent (Student): Pass =# inherited subclasses can implement modifications # unless __slots__ is also defined in a subclass, so that the subclass instance allows the defined property to be its own __slots__ Add the __slots__ of the parent class.
@property
Python's built-in @property decorator is responsible for turning a method into a property call
class : def __init__ (self,name,width,length): self.name=name self.width=width self.length= length @property def area (self): return self.width * self.lengthr1=room ('Alex', ' a ')print(R1.area)
Custom class Python's class has a lot of special later Han books to help us customize our class.
__str__ __repr__
# define a student class to print an instance class Student (object): def __init__ (self, name): = nameprint(Student ('micheal'))
<__main__. Student object at 0x00000000028d1128> #__str__ method is to make this line clearer
Commonly used __str__ __repr__ usage is to combine them with
class Student (object): def __init__ (self, name): = name def__str__(self): return' Student object (name=%s)' % self.name __repr____str__
The "' str ' function or the print function--->obj.__str__ () repr or the interactive interpreter--->obj.__repr__ () if __str__ is not defined, then __repr__ is used instead of the output note: The return value of both methods must be a string, otherwise throw an exception "'
Python-style getter setters are also implemented in the property
Class Foo: @property: def aaa (self): print (' Get with this ') @AAA. Setter def aaa (self , value): print (' Set with this ') #只有在属性AAA定义property后才能定义AAA. Setterf1 = Foo () F1. AAA # call GETF1. Aaa= ' AAAASD1 ' #调用 set
Another form of implementation is __getattr__ __setattr__ ()
classfoo:x=1def __init__(self,y): Self.y=ydef __getattr__(self, item):Print('----> from getattr: The attribute you are looking for does not exist') def __setattr__(self, Key, value):Print('----> from setattr') #Self.key=value #这就无限递归了, you think about it. #Self.__dict__[key]=value #应该使用它#__setattr__ Adding/Modifying a property will trigger its executionF1=foo (10)Print(F1.__dict__)#because you rewrite the __setattr__, any assignment operation will trigger its operation, you do not write anything, is not assigned at all, unless you directly manipulate the property dictionary, otherwise you can never assign a valueF1.z=3Print(F1.__dict__)#__getattr__ fires only when a property is called with a point and the property does not existF1.xxxxxx
__call__ when we invoke an instance method, we may use the isinstance (obj) call or use the __call__ method.
Use of enumeration classes
from Import Enum class Gender (Enum): = 0 = 1class Student (object): def__init__(self, Name, gender): = name == Student ('Bart', Gender.male)
The contents of the meta-class are vaguely understood. Read through the Liaoche of the teacher's meta-class introduction
Https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 0014319106919344c4ef8b1e04c48778bb45796e0335839000
Python object-oriented high-order