This article mainly introduces the Python class details and examples of relevant information, the need for friends can refer to the following
Python class explanation
Class
1. A class is a data structure that you can use to create instances. (Typically, the class encapsulates the data and the methods that are available for that data)
The 2.Python class is a callable object, that is, a class object
3. Classes are typically defined at the top level of the module so that class instances can be created anywhere in the source code file defined by the class.
4. Instance initialization
Instance = ClassName (args ....)
Classes can use the Init and Del two special methods when instantiating.
Class ClassName (Base): ' Class documentation string ' #类文档字符串 class suite #类体
Base: A superclass is one or more collections of parent classes for inheritance
The class body can include: Declaration statements, class member definitions, data properties, methods
If the class does not have an inheritance relationship, the base in parentheses is not provided
Class FirstClass (): spam = #类数据属性 def display (self): #类方法 print Self.spam x = FirstClass () # Create class instance X.display () #方法调用 >>> dir (firstclass) >>> [' Doc ', ' module ', ' Display ', ' spam ']
The class statement is like Def, which is executable code, and classes are not created until the class statement is run
Class statement, any assignment statement will create a property
Each instance object inherits the properties of the class and obtains its own namespace
Python class methods and calls
An instance (object) contains properties
In OOP, instances are like records with "data", and classes are "programs" that process these records
Invoking a method through an instance is equivalent to invoking the method of the owning class to handle the current instance. For example, the previous code example, X.display () is automatically converted to Firstclass.display (x), the method that invokes the class to process the instance X
Therefore, each method in a class must have a self parameter, which implies the current instance's meaning
Assigning an assignment to the Self property within a method results in each instance's own properties
Python stipulates that there is no instance, the method is not allowed to be called, this is the concept of ' bind ' (binding)
The assignment statement in the class statement creates a class attribute, as in the previous example spam
Assigning the special parameter self to a method in a class method creates an instance property
Python Builder
When you create an instance, Python automatically calls the Init method in the class to provide an invisible property for the instance
The Init method is called the constructor
If the Init method is not defined in the class, the instance is created simply as a simple namespace.
The first argument to Init must be self, and the self variable is used to refer to the instance that the method is bound to in the class instance method. Because an instance of a method is always passed as the first argument in any method call, self is selected to represent the instance. You must put self in the method declaration, but you can not use the instance (self) in the method. If you don't use self in your method, consider creating a regular function unless you have a particular reason. After all, your method code does not use an instance, it does not associate its functionality with the class, which makes it look more like a regular function. In other object-oriented languages, self may be referred to as this.
Init cannot return any objects
destructor: Del
Constructors are necessary, and destructors are often not considered (the Python interpreter recycles itself)
Class MyClass (): Def init (self, name): self.name = name print ' My name is ' + Self.name def del (self): print S Elf.name + ' is dead. ' I1 = MyClass (' Shaw's ') >>> My name is Shawdel i1>>> Shaw ID dead.
Special properties of the class
Use Dir () or Dict to view the properties of a class or instance
Doc: Get the document string
Base: Get all parent classes
Module: Modules in which the class resides
Name: Names of classes to which the instance belongs
Variables available in the Python class method
Instance variable: Self. Variable name
Local variable: A variable created inside a method that can be used directly
Static variable: A variable defined in a class. Class name. Variable name
Global variables: Direct use
Inherited
Inheritance describes how the properties of a base class are ' inherited ' to derived classes
Subclasses can inherit any property of his base class, including data properties and methods
A class that does not specify a base class, which has a base class called object by default
Python allows multiple inheritance (multiple parent classes can be inherited)
Thank you for reading, hope to help everyone, thank you for the support of this site!