Finishing class

class

Practical features of the object is a combination of

It is the same class and object feature series combination of skills

How to define a class

First, the definition of class, after calling the class to create objects

The syntax for defining classes

class 类名:
 对象相同的特征
 对象相同的技能
class 用来定义类的,class后跟类名

Note: Class names must follow the hump nomenclature

What happened in the definition of the class

  1. When the class definition, will have an empty namespace

  2. Namespace inside would like all the names, thrown into the class

    Note: In the class definition phase has produced good name space

    Performs like internal code execution when file python

Namespace class name will point to the class

Class. "" Features or skill manner inside the class variable value changes are

. "" I will point to the internal class name space, so as to obtain variable values

Action class has

Add, delete, change,

Objects

How to generate objects:

grammar:

Class name () call generates an object class

Produced namespace

  1. Class namespace in the class definition phase has produced a
  2. Namespace object class is generated when calling

__init__ magic function when calling the class, automatically triggers the function

Add properties to the object of way

self.name = x

stu1.age = 10

View namespaces

__dict__

Find the order of objects and classes

  1. Object Property, if the object itself, it is a priority to find the object of their own
  2. If the object is not, then take to find a class which, if no category is given

Special about object binding method

Class internal function is mainly used to the subject

  1. A class-based call functions, the function is just an ordinary function,

Normal function will receive several parameters have to pass a few parameters

  1. Called by the object to call a bound method

    Call the different objects bound method, it will be a different object passed in the binding process

inherit

Inheritance is a series of classes with the same characteristics and skills

Inherit the properties and methods of the parent class

__bases__ find the current parent

First abstract and then inherit

When not complicate the analysis first and then inherited abstract

In the context of inheritance, object attributes search order

  1. Object lookup property will first look in the name of the object in space
  2. If the object does not, then go back inside to find class
  3. If the current class is a subclass, and no objects to find the property, will go to the parent class Find

Note: The object lookup property, if by the subclass, regardless of the parent class there, whichever subclass

Derivation

Derived refers to the property of the child class inherits the parent class, and derive new property

Subclassing new properties, the same as if the properties of the parent class, subclass places prevail

Subclass to derive new property, both with the parent class

Superclass .__ init__

super().__init__

The new class of Classic

The new class inherits object

Classic class does not inherit object

vale ()

View current order of succession of the class

Diamond inheritance

In the case of multiple inheritance

Classic depth-first

The new class of breadth-first

combination

inherit

Relationship classes and relationship classes, subclasses of the parent class

combination

The relationship between objects and object, an object has another object

@property

When the call is not available if parentheses to call the statement in judge

Polymorphism and polymorphism

When using a different class of things, using the same method name

Abstract class, abc @abc

All subclasses follow the way of the parent class, use the abc module to be dead way

Duck type (style)

As polymorphic as work, but there is no inheritance, the object of interest method

Polymorphic virtuoso

The method of the same class from the pile, using a defined function of the abstract class function call and Unified

restriction of visit

A variable or function at the beginning of the class name _ __ __ becomes variable name

@classmethod

  1. Bound method object
  2. Binding methods of the class

@staticmethod

Non-binding approach

The method of interior decoration can, so that the method is not bound to the target that is not bound to the class, a general function becomes

uuid

An encryption module, uuid4 random numbers, uuid1 based on the timestamps

__class__

Properties of the object, the object to obtain the current class

isinstance

Parameter interpretation whether one example of a parameter 2

issubclass

1 determines whether the parameter is a parameter subclass 2

reflection

Or verify operation on the attributes of the object class as a string or

Hsattr

Determining whether the string is a property of an object or class

getattr

Get property of object or class

setattr

Setting properties of the object or class

delattr

Delete the object or class of property

Magic method (built-in methods class)

__init__ triggered when calling class

__str__ will be triggered when the print object

__del__ the object is destroyed before the implementation of the method, which will last execution

__getattr__ will only trigger in the object. attribute when attribute is not the case

__setattr__ will be triggered when "object. attribute = attribute value"

__call__ is triggered when the object has been called

__new__ will be executed before the execution __init__

Singleton

class File:
    __instance = None
    @classmethod
    def singleto(cls,file_name):
        if not cls.__instance:
            obj = cls(file_name)
            cls.__instance = obj
            return cls.__instance

Guess you like

Origin www.cnblogs.com/kaizi111/p/11669245.html