# Python class static methods and class method differences
# # First Look at the syntax, Python class syntax There are three methods, instance methods, static methods, class methods.
This article is written by Huanggo Python training Huanggo.
# Coding:utf-8
Class Foo (object):
"" "Class three method syntax form" "
def instance_method (self):
Print ("is an instance method of class {} and can only be called by an instance object". Format (Foo))
@staticmethod
Def static_method ():
Print ("is static method")
@classmethod
def class_method (CLS):
Print ("is class method")
foo = foo ()
Foo.instance_method ()
Foo.static_method ()
Foo.class_method ()
Print ('----------------')
Foo.static_method ()
Foo.class_method ()
An instance method can only be called by an instance object, a static method (a method decorated by @staticmethod), a class method (a method that is decorated by @classmethod), and is called by an instance object of a class or class.
instance method, the first parameter must be passed to the instance object by default, and is generally used with self.
Static methods, parameters are not required.
Class method, the first parameter must be passed by default, and is generally used in CLS.
# # static methods, class methods use differences or use scenarios
1. The class method is used in simulating Java to define multiple constructors.
Because there can be only one initialization method in a Python class, the class cannot be initialized in different cases.
Refer to Django https://docs.djangoproject.com/en/1.9/ref/models/instances/
Take a look at the following code.
# Coding:utf-8
Class Book (object):
def __init__ (self, title):
Self.title = Title
@classmethod
DEF create (CLS, title):
Book = CLS (title=title)
Return book
Book1 = Book ("Python")
Book2 = book.create ("Python and Django")
Print (Book1.title)
Print (Book2.title)
In particular, the static method can also achieve the above function, when the static method to write the name of the class every time, inconvenient.
2. A static method method calls a static method in a class.
The following code, a static method calls another static method, and if you use a class method to call a static method, you can let the CLS replace the class,
Make the code look leaner. Also prevents the class name from being modified without modifying the original class name in the class definition.
# Coding:utf-8
Class Foo (object):
X = 1
Y = 2
@staticmethod
def averag (*mixes):
return sum (MIXES)/Len (mixes)
@staticmethod
Def static_method ():
Return Foo.averag (foo.x, FOO.Y)
@classmethod
def class_method (CLS):
Return Cls.averag (CLS. X, CLS. Y
foo = foo ()
Print (Foo.static_method ())
Print (Foo.class_method ())
3. Differences in inheritance Classes
As can be seen from the following code, if the subclass inherits the method of the parent class, the subclass overrides the static method of the parent class.
The instance of the subclass inherits the Static_method static method of the parent class, calls the method, or invokes the method and class properties of the parent class.
An instance of a subclass inherits the Class_method class method of the parent class, calls the method, calls the method of the subclass, and the class property of the child class.
# Coding:utf-8
Class Foo (object):
X = 1
Y = 2
@staticmethod
def averag (*mixes):
return sum (MIXES)/Len (mixes)
@staticmethod
Def static_method ():
Return Foo.averag (foo.x, FOO.Y)
@classmethod
def class_method (CLS):
Return Cls.averag (CLS. X, CLS. Y
Class Son (Foo):
X = 3
Y = 5
@staticmethod
def averag (*mixes):
return sum (mixes)/3
p = Son ()
Print (P.static_method ())
Print (P.class_method ())
# 1.5
# 2.6666666666666665
Share!
Huanggo written in 2016-5-24 pm Beijing
[Click Huanggo python Training preview video playback address] (HTTPS://GITHUB.COM/PYTHONPEIXUN/ARTICLE/BLOB/MASTER/PYTHON_SHIPING.MD)
The difference between static and class methods for Python classes