Instantiating object Binding Properties
S.name = ' Michael ' # dynamically binds an attribute to an instance
Class Binding method---All instantiated objects can be called
Student.set_score = Set_score
Instantiates an object binding method---only that object can be called
The From types import methodtype# binding method requires the use of Methodtype
S.set_age = Methodtype (Set_age, s) # Bind a method to an instance
Python's built-in @property decorator is responsible for turning a method into a property call.
Turn a getter method into a property, just add @property on it, at this point, the @property itself created another adorner @score.setter, responsible for a setter method into a property assignment, so we have a controllable property operation
Python allows the use of multiple inheritance, so mixin is a common design.
Only allow single-inherited languages (such as Java) to use the mixin design
What if I see the source code for built-in functions? For example, the source of Len (), why an object can use Len () must define the __len__ method within the object's class
Enum class enum
@unique adorners can help us check that there are no duplicate values.
Use Type () to create a class object, passing in 3 arguments sequentially: (You can generate class dynamically)
The name of the class;
Inherited parent class Collection, note that Python supports multiple inheritance, if there is only one parent class, do not forget the single element of tuple notation;
Class's method name and function binding, where we bind the function fn to the method name hello.
Foo = type (' foo ', (object,), {' Func ': func})
>>> def fn (self, name=' World'): # define function First ... print ('Hello,%s.'%name) ...>>> Hello = Type ('Hello', (Object,), Dict (HELLO=FN) # Create Helloclass>>> h =Hello ()>>>H.hello () Hello, world.>>>print (Type (Hello))<class 'type'>>>>Print (Type (h))<class '__main__. Hello'>
Simple example of type Creation object
classMyType (type): #创建超级类type的子类mytype def __init__ (self,*args,**Kwargs): #type里的self相当于foo类; This is not the same as the self of the parent class of the child class. Print ('123','Execute when creating Foo Class (defining Class)') def __call__ (self,*args, * *Kwargs): Print ('mytype__call__','when the 1.foo class creates an object, Foo (), Foo as the type Object, foo () executes __call__ () in the type, and __call__ calls the __new__ () method of the Foo class') self.__new__ ('FDS'the self.__init__ () #执行foo类的__init__ () method created in the #其实对象是在__new__ () methodclassFooObject, metaclass=mytype): #创建类时, modify to use MyType to create Def __init__ (self): print ('foo__init__') def __new__ (CLS,*args, * *Kwargs): Print ('foo__init__') return 'Object'F created in the #其实对象是在__new__ () method=foo () #之前的说法: Class name () executes the __init__ () method of the class # There's actually a few steps in the middle #1. Foo is the class name, but it is also an object of type class, so the object () executes the __call__ () method in the type #2The __call__ () execution type invokes the __new__ method in the Foo class---create Foo class object #3The __call__ () execution type invokes the __init__ method in the Foo class.
Type the process of creating class
The singleton mode exists to ensure that only a single instance exists in the current memory, avoiding memory waste!!!
When you create an object, you can no longer use it directly: obj = Foo (), and you should call a special method: obj = Foo.dl ().
The essence is actually to define a static method, to determine whether there is already a singleton object, if there is a direct return of the Singleton object, if not, create a singleton object, and then return to a single case
Object
The last is the description of the special variable
Liaoche-Object-oriented advanced programming-using meta-class-metaclass and custom ORM framework-to review
Http://www.cnblogs.com/wupeiqi/p/4766801.html
https://www.cnblogs.com/maskice/p/6493404.html (Leak check)
Python Basics-Object-oriented advanced programming