Object-oriented knowledge finishing Review

Object-oriented knowledge finishing Review

An object-oriented basis

The core object-oriented programming word is the object, the object is a combination of attributes and methods, and all are subject in Python.

Advantages: scalability

Disadvantages: high complexity of programming

1, classes and objects

Object: combination of properties and methods

Class: combination of properties and methods of pile

python is the first in the class and then have an object in real life, then there is the first class objects

Class name () is the class generates an object can be instantiated

class Person:
    school = 'xxx'
    def __init__(self):  # 对象初始化方法,对象的绑定方法,对象来调用会把对象传过来
        self.name = 'xxx'
p = Person()  # 类实例化产生对象会调用类的__init__方法

2, find the property:

  • Data attributes: the object itself to find --- "category ---" parent (sort used mro list) --- "object ---" is not on the incorrect report
  • Find method: Bind method object --- "--- the parent class in accordance with mro Find a list of" object --- "is not on the incorrect report

3, binding method:

  • Within the class is defined, the function is not to be decorated any decoration, a method is
  • To the object to use: object method () default pass over the object itself
  • Class to call is a normal function, there are several values ​​to pass several values

4, interaction between objects:

  • The object passed to another object, the object ---- write operation before the people and dogs shootout examples

Built-in properties 5, class

- class name __name__: class name (string)

- class name __doc__: Class documentation strings

- class name __bases__: The class of all parent classes

- the class name __base__: The first class of the parent class

- the class name __dict__: class namespace dictionary (properties and methods)

- class name __module__: class where the module definition

- class name. __class__: Examples of a corresponding class (there is only the new class)

6, three characteristics: inheritance, polymorphism, encapsulation

- Inheritance:

- Write to inherit the class in parentheses after the class name, separated by commas

--py support multiple inheritance (attribute search order)

- Modern and Classic classes

- call the new class inherits the class of object (py3 in all new class)

--py2 the only new-style class, classic sort of argument

- Find diamond issue the order

- new categories: breadth-first

- Classic: depth-first

--mro list

--super () special object, call the properties and methods of the parent class (in strict accordance with mro find a list)

The difference --self and super: self must find fundamentally, super () from the current location of the search list in accordance with mro

- name of the class method () is a normal function, this is not sort used mro list, but the use of naming names

- Derived: subclass extra properties and methods

- polymorphism and polymorphism

- polymorphism: a variety of forms of things

- Polymorphism: Perform the same methods of the same things, manifested result is not the same

- two restriction method subclass must implement what way

- a module abc

- raise Throws

- Ducks Type: walks like a duck, you're a duck

- len () is actually calling the object. __len__()

- Package

- the hidden properties and methods

- __ begin with, will be hidden

- I do not actually hide (do a sex change)

- Hide property: to ensure data security

- concealment methods: isolation complexity

- property decorator: data attributes into the packaging

- the other two decorators

- classmethod: binding method of the class, the class can be invoked automatically to the incoming class (objects can also be invoked)

- staticmethod: static method, anyone can call, do not automatically pass parameters

Second, the object-oriented high-order

Moto类

  • The concept of metaclass: class because all are objects, classes are objects, Yuan class constructor class, and therefore able to instantiate the class is to get metaclass

  • type is the most top-level meta-class

  • py3 all classes are inherited from the object (including the metaclass type is inherited pbject of)

  • object class is created by the meta-class type, and type class is also created by the metaclass type of

  • Custom yuan class: inherited type of class, called the metaclass, so we can customize the yuan class (class Person (metaclass = Mymeta) is specified metaclass my custom

  • class Mymeta(type):
        # 通过重写__call__来控制对象的产生
            def __call__(self,*args,**kwargs):
            # 这个会在类加括号(实例化产生对象)的时候触发,用来控制对象的创建
            # 第一步:调用Person类的__new__方法,来产生一个空对象
            # obj=object.__new__(self)
            obj = self.__new__(self) # 推荐使用这种方式来创造空对象,以为这种方式会检索类和类的父类,而object.__new__则是直接跨过了他们三个
            # 第二步:调用Person类的__init__,完成对象初始化
            obj.__init__(*args,**kwargs)
            # 这里写初始化对象的语句
            # 第三步:返回初始化好的对象
            return obj
            pass
    
        def __new__(cls,name,bases,dic):
            # 控制类的产生
            # 产生空对象(空类),在这里面生成的其实并不是空类,是有数据的类了
            # 这里写修改类的名称空间的代码,通过修改dic来控制产生的类的名称空间
    
            _class=type.__new__(cls,name,bases,dic) # 完成类的初始化,并且把name,bases,dic这些东西放入
            return _class  # 把产生的类返回出来
            pass
       # Mymeta(name,bases,dic)实例化产生类,会调用type的__call__,内部调用Mymeta的__new__和__init__,
    
       def __init__(self):
            # 控制类的初始化
            pass     
    
    class Person(metaclass=Mymeta): 
            pass
  • __new__And __init__the difference between

    1, __new__create an empty object (class are objects)

    2, __init__initialize the object

  • Original 类中:

    1, custom metaclass rewriting __call__method: to create the control objects generated from the class defined metaclass generated instantiated

    2, rewrite custom metadata class __new__methods: most root generated control class, in fact, the most fundamental nature is not it, is the metaclass type __call__, but we can not modify the type, we can only rewrite custom metaclass __new__method

    3, rewrite custom metaclass __init__Method: Create Custom Control element generated class is instantiated class, but is __new__after.

  • After the property has metaclass search order

    Object layer: starting with the object itself to find --- "category to find ---" parent class by mro list to find --- "not on the incorrect report

    Class level: start with the class itself to find --- "parent class by mro find the list ---" custom metaclass find --- "metaclass type in to find ---" not on the incorrect report

  • issubclass和isinstance

    --issubclass (): The first category is determined is not a subclass of the second class, the result is True or False

    --isinstance (): The first parameter is judged not a second parameter (s) of the object, the result is True or False

  • Reflection (by acquiring a string, modify, delete object property or method)

    - hasattr (): determining whether an attribute in an object, returns True or False hasattr (object attribute)

    - getattr (): get property method or a string, to obtain the corresponding property or method returns getattr (object properties or methods)

    - setattr (): Set a property or method setattr by a string (object, method of the 'name', method name)

    - delattr (): Delete a string property or method delattr (object properties or methods)

  • Built-in method (magic method)

    Point intercept method: object method will trigger

    - __getattr__: Object added .to obtain the value, once the process proceeds to fail to

    - __setattr__: The object plus .assignment, once you fail to enter the method

    - __delattr__: Object plus .delete a value, enter the method

Singleton

  • The concept: multiple instances of the point is the same memory address, to get the same object

  • Advantages: saving space

  • Four methods to achieve single-mode embodiment

  • 1, a binding defined class that implements the method of Singleton

    2, define a decorative implemented Singleton

    3, define a metaclass implement singletons

    4, by introducing single-block embodiment mode (module Python natural singleton)

Guess you like

Origin www.cnblogs.com/zhuangyl23/p/11503338.html