This article mainly introduces the detailed description of object-oriented programming in Python (I ), this article describes how to create a class, instantiate a class, class attribute, built-in attribute of a special method, static variable attribute, instance variable attribute, method attribute, static method, and class method, for more information, see
Create class
The Python class is created using the class keyword. The declaration of a simple class can be a keyword followed by the class name:
The code is as follows:
Class ClassName (bases ):
'Class documentation string' # 'class document string'
Class_suite # class body
Instantiation
Instantiate a class by class name followed by a pair of parentheses
The code is as follows:
Mc = MyClass () # instantiate class initialization class
'Int () 'constructor
Def _ int _ (self ):
Pass
Note: self is similar to the Java this keyword, and its code points to the reference of its own instance.
Class attributes
The attributes of python are different from those of Java, C ++, and other object-oriented languages. the attributes of python include data members, function elements, and can be accessed through periods.
Special Data built-in attributes
C.NameClass C name (string)
C.DocDocument string for Class C
C.BasesTuples of all parent classes of Class C
C.DictClass C attributes
C.ModuleModule of class C definition (New in version 1.5)
C.ClassClass corresponding to instance C (only in the new class)
Built-in attributes of special methods
Dir (): get the list of class attributes or instance attribute names.
Static variable attributes
Defined directly in class scope
The code is as follows:
Class C (object ):
Foo = 100.
Instance variable attributes
The instance attributes of python are different from those of Java and C ++. in Java and C ++, the instance attributes must be declared/defined first, while the python instance attributes are dynamically created. You can set the attributes of an instance at any time after the instance is created, or in the code that can access the instance. Structure
Init () is one of the key points to set these attributes.
The code is as follows:
Def _ init _ (self, name, data ):
Self. name = name
Self. data = "123'
Note: self is similar to the Java this keyword, and its code points to the reference of its own instance.
Method property
It can be divided into the instance method and class method. the instance method only belongs to one instance, and the class method belongs to both the class and the instance.
Instance method
The code is as follows:
Class MyClass (object ):
Def myNoActionMethod (self ):
Pass
Note: self is similar to the Java this keyword, and its code points to the reference of its own instance.
Static method
Static methods are class-level methods that can be called directly without instantiating classes. There are two methods to define
● Decorator (commonly used)
The code is as follows:
@ Staticmethod
Def foo ():
Print 'Call static method'
● Built-in functions
The code is as follows:
Def foo ():
Print 'Call static method'
Foo = staticmethod (foo) # static method
Class method
A static method is a class-level method. unlike a static method, it must display the passed cls class parameters. if you need to call other static methods in the class or functions of the class method, to be defined as a class method. similar to static methods, two methods are also defined.
● Decorator (commonly used)
The code is as follows:
@ Classmethod
Def bar (cls ):
Print 'Call class method and access static varible (staticVar): ', cls. staticVar
● Built-in functions
The code is as follows:
Def bar (cls ):
Print 'Call class method and access static varible (staticVar): ', cls. staticVar
Bar = classmethod (bar) # class method
Instance details
The code is as follows:
#! /Usr/bin/python
# Coding = UTF-8
Class Target (): # define the class Target
'This is Target definition '# define _ doc _ attributes
StaticVar = 'v1. 0' # define static variables
Def _ init _ (self, name = 'default', data = 0): # define the constructor
Self. name = name # instance variable
Self. data = data # instance variables
Print "init instance"
Def main ():
Print "this is a test function"
'''
You can use the decorator to define static methods.
@ Staticmethod
Def foo ():
Print 'Call static method'
'''
Def foo ():
Print 'Call static method'
Foo = staticmethod (foo) # static method
'''
You can use the decorator to define class methods.
@ Classmethod
Def bar (cls ):
Print 'Call class method and access static varible (staticVar): ', cls. staticVar
'''
Def bar (cls ):
Print 'Call class method and access static varible (staticVar): ', cls. staticVar
Bar = classmethod (bar) # class method
# The main () method takes effect only when this module is called.
If _ name _ = '_ main __':
Main ()
# Instantiation
Target = Target ('AAA', 123)
Print 'name is: ', target. name
Print 'data is: ', target. data
# Print _ doc _ attributes
Print 'target. _ doc _ is: ', target. _ doc __
# Print the _ dict _ attributes
Print 'target. _ dict _ is: ', Target. _ dict __
# Print static variables
Print 'staticvar is: ', Target. staticVar
# Print the built-in function dir ()
Print 'dir () is: ', dir (Target)
# Call static methods
Target. foo ()
# Call methods
Target. bar ()
Output
The code is as follows:
This is a test function
Init instance
Name is: aaa
Data is: 123
Target. _ doc _ is: This is Target definition
Target. _ dict _ is: {'_ module _': '_ main _', 'foo ': , 'Bar ': , 'Staticvar': 'v1. 0', 'main ': , '_ Doc _': 'This is Target definition ',' _ init __': }
StaticVar is: v1.0
Dir () is: ['_ doc _', '_ init _', '_ module _', 'bar', 'foo ', 'main', 'staticvar ']
Call static method
Call class method and access static varible (staticVar): v1.0