Analysis of classes in python, and analysis of python Methods
This article analyzes some methods of classes in python and shares them with you for your reference. The specific analysis is as follows:
Let's take a look at the following code:
class Super: def delegate(self): self.action() class Provider(Super): def action(self): print 'in Provider.action' x = Provider() x.delegate()
The running environment of this instance is Python2.7.6.
The running result is as follows:
In Provider. action
Define the delegate () method in the Super class, call self. action in the delegate class, and implement the action Method in the Provider subclass. When a subclass calls the delegate method of the parent class, it actually calls its own action method ..
In a word:
Here the subclass implements the expected action method in the parent class delegate.
Let's take a look at the following code:
class Super: def delegate(self): self.action() def method(self): print 'super method' class Inherit(Super): pass class Replace(Super): def method(self): print "replace method" class Extended(Super): def method(self): print 'in extended class' Super.method(self) print 'out extended class' class Provider(Super): def action(self): print 'in Provider.action' x = Inherit() x.method() print '*'*50 y = Replace() y.method() print '*'*50 z = Extended() z.method() print '*'*50 x = Provider() x.delegate()
The running result is as follows:
super method ************************************************** replace method ************************************************** in extended class super method out extended class ************************************************** in Provider.action
Inherit the methods of the parent class, replace the methods of the parent class, and extend the methods of the parent class.
The Super class defines the delegate method and expects the subclass to implement the action function. The Provider subclass implements the action method.
I believe this article has some reference value for everyone's learning about Python programming.
Teaches conceptual issues in the python class
The concept of classes in Python programming can be compared to the description of a certain type set. For example, "human" can be considered as a class, then we use the human class to define every specific person-you, me, and others as their objects. The class also has attributes and functions. attributes are some characteristics of the class, such as human attributes such as name, height, and weight. The specific values are different for each person; A function is a behavior that can be implemented by a class, such as eating, walking, and sleeping.
Classes are generally defined:
Class name [(parent class name)]: [member functions and member variables],
The class name is the name of this class, and the parent class name is optional. After the parent class name is defined, the subclass has the corresponding attributes and methods of the parent class. When defining an object as a class, the _ init _ constructor is called first to initialize the attributes of the object and the attributes of the class (member variables) can be defined in the constructor, as long as the object pointer is added to the definition. When an object is destroyed, the _ del _ destructor is called. when defining a class member function, a variable must be used by default (similar to the this pointer in C ++) indicates the object defined by the class. The variable name can be customized. Generally, the self variable is used.
The base class is similar to the parent class.
The name of a superclass is related to the super keyword. In fact, if the method or variable does not exist in the current class, the back-to-back will follow the inheritance chain until the object class.
Instantiation is to create an object from a class.
Some minor issues in Python
The _ init _ method runs immediately when an object of the class is created. This method can be used to initialize your object. Note that the start and end of the name are double underscores. Use the _ init _ method example 11.3 to use the _ init _ method #! /Usr/bin/python
# Filename: class_init.py
Class Person:
Def _ init _ (self, name ):
Self. name = name
Def sayHi (self ):
Print 'hello, my name is ', self. name
P = Person ('swaroop ')
P. sayHi ()
# This short example can also be written as Person ('swaroop '). sayHi () (source file: code/class_init.py) Outputs $ python class_init.py
Hello, my name is Swaroop how it works here, we define the _ init _ method as a parameter name (and a common Parameter self ). In this _ init _, we only create a new domain, also known as name. Note that they are two different variables, even though they have the same name. Point Numbers allow us to differentiate them. The most important thing is that we didn't specifically call the _ init _ method, but when creating a new instance of a class, we included the parameters in parentheses following the class name, to pass the _ init _ method. This is an important part of this method. Now, we can use the self. name field in our method. This is verified in the sayHi method. Comments to C ++/Java/C # programmers
The _ init _ method is similar to constructor in C ++, C #, and Java.
_ Del __
Similar to destructor
#! /Usr/local/bin/python
Class Study:
Def _ init _ (self, name = None ):
Self. name = name
Def _ del _ (self ):
Print "Iamaway, baby! "
Def say (self ):
Print self. name
Study = Study ("zhuzhengjun ")
Study. say ()