The use and difference of class method, class instance method, static method in Python2011-03-24 11:58:06| Category: Python | Tags: Python class method static method | report | Font size SubscriptionTransfer from http://stonelee.javaeye.com/blog/726449How to use:
ObjectiveFor beginners, Python is really easy to use, after all, dynamic type language, no definition can be used, the type of arbitrary conversion is not too convenient, so Python used to write small scripts, bots and so on, no problem.However, once used to develop a slightly larger project, such as building a Web application, you will encounter some problems, and generally inexperienced people will fall i
New and Legacy classes The new Python class is introduced in version 2.2, and we can call the previous Class A classic or old class. Why introduce the new style class in 2.2? The official explanation is: In order to unify classes (class
Note: The Python2.7 is used.
One, instance method
An instance method is a method that an instance of a class can use. As follows:
Copy Code code as follows:
Class Foo:
def __init__ (self, name):
Self.name = Name
def hi (self):
Print Self.name
if __name__ = = ' __main__ ':foo01 = Foo (' Letian ')Foo01.hi ()Print type (Foo)Print type (FOO01)Print ID (foo01)Print ID (Foo)
The
The new Python class is introduced in version 2.2, and we can call the previous Class A classic or a legacy class.
Why introduce the new style class in 2.2? The official explanation is:
In order to unify classes (class) and types
method 'c = Child () # Instantiate a subclassC.childmethod () # calls a subclass's methodC.parentmethod () # Calling the parent class methodC.setattr (200) # The method of calling the parent class againC.getattr () # The method of calling the parent class again The above code execution results are as follows: Calling
process-oriented programming and object-oriented programmingprocess-Oriented programming : The core is the process, process on the steps to solve the problem, based on the idea of the design process is like a pipeline, is a mechanical way of thinking Advantages: Simplification of complex problems, flow ofCons: Poor scalabilityObject-Oriented programming : The core is the object, the object is a combination of characteristics (variables) and skills (functions), is a kind of God-like way of thinki
The initialization method for classes in Python is __init__ (), therefore, the initialization method of the parent class subclass is this, if the subclass does not implement this function, the initialization function of the parent class is invoked when the subclass implements this function, and the parent class is over
(self): print ("%s was eating"%self.name) if __name__ = = ' __main__ ': p = person ( ' John ') p.eat (p)Run results
Class method
Class methods are defined by @classmethod.Class methods can access only class variables and cannot access instance variables#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar"
is used, Python searches from left to right, that is, when the method is not found in the subclass, the parent class is searched from left to right to include the method
#另一个类, the preparation of multiple inheritance before class speaker (): topic = ' name = ' def __init__ (self,n,t): self.name = n Self.topic = T def speak (self): print ("I a
Example Analysis of Class usage in Python and Analysis of python instances
This example describes the usage of the Class in Python. We will share this with you for your reference. The details are as follows:
Although Python has ad
','Jxz') My.exsql ('select * from Stu;')Print('I am the last line of code')In the constructor, make the database connection, do the exception processing, execute the SQL statement in the executing SQL statement function, the operation of the destructor closes the cursor and the database connection.In connection with the database, there is a new knowledge point: autocommit=true auto-commit, which was not mentioned in the previous database connection use, instead of the need to determine whether t
The new class is the class person (object): This form, from the beginning of the py2.2The new class adds: is the attribute's name. is the attribute's docstring. __get__ (object is fromobject. __set__ (objectObject to value.__delete__ (objectobject.The emergence of the new class, in addition to a large number of method
Attributes are divided into instance properties and class propertiesMethods are divided into ordinary instance methods, class methods, static methodsBoth static and class methods of Python can be accessed by a class or instance, and the difference is obvious:1) Static method
Python class variable and member variable usage example tutorial, python instance tutorial
This article provides examples to illustrate the usage of class variables and member variables in python, which has some reference value for Pytho
Python inherits that subclasses can override various methods of the parent class, including the __init__ method.If you want to overwrite the __init__ method of the parent class, and you want to refer to the __init__ method of the parent class in the overridden method, such as adding some properties to the __init__ meth
Tag: Cal Call Limit add nbsp Ace Pre Attribute Property__slots__ because Python is a dynamic language, any instance can dynamically add properties at run time. If you want to limit the properties that you add, for example, the student class only allows you to add the 3 properties of name, gender, and score, you can take advantage of a special __slots__ of Python.
Python notes: class and inheritance, python notes inheritance
#-*-Coding = UTF-8 -*-
# The file encoding syntax is: coding [: =]/s * ([-/w.] +)
# If no encoding is specified, the default value is ASCII.
# Note that the encoding of physical files must also conform to the encoding specified by the language.
# More can refer to: http://python.org/dev/peps/pep-
of several parts, we will do a division, set up a department to do a part of the function of another department to do another part. Departments can not understand other departments, as long as the completion of their own part of the matter is OK.Second, object-oriented characteristicsClasses: Classclass, compared to the real world, is a kind, a model.A class is an abstraction, a blueprint, a prototype for a class
Explanation of class and object descriptors in Python, and explanation of python Descriptors
Descriptors is an esoteric but important part of the Python language. They are widely used in the Python kernel, and mastering Descriptors will add an additional skill to the toolbox
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.