As a powerful object-oriented open-source programming language, the Python programming language has outstanding application features and greatly facilitates the application of developers. In the course of learning, we can accumulate experience from relevant practices to master the application skills of this language. For example, the related content of the Python application skills introduced today is an important experience.
Python application Tips 1. self, cls is not a keyword
In python, self and cls are not keywords and can be replaced by any variable you write.
Code 1
- class MyTest:
- myname = 'peter'
- def sayhello(hello):
- print "say hello to %s" % hello.myname
- if __name__ == "__main__":
- MyTest().sayhello()
- class MyTest: myname = 'peter' def sayhello(hello): print "say hello
to %s" % hello.myname if __name__ == "__main__": MyTest().sayhello()
In code 1, self is replaced with hello, which achieves the same effect and can be replaced with this, which is commonly used in java.
Conclusion: self and cls are just written as agreed in python. They are essentially just a function parameter and have no special meaning.
Any object that calls a method will pass itself as the first parameter in the method to the function. (Because everything in python is an object, when we use Class. method (), the first parameter is actually our agreed cls)
Python application Tip 2. class definition can be dynamically modified
Code 2
- class MyTest:
- myname = 'peter'
- def sayhello(self):
- print "say hello to %s" % self.myname
- if __name__ == "__main__":
- MyTest.myname = 'hone'
- MyTest.sayhello = lambda self,name: "I want say hello to %s" % name
- MyTest.saygoodbye = lambda self,name: "I do not want say goodbye to %s" % name
- print MyTest().sayhello(MyTest.myname)
- print MyTest().saygoodbye(MyTest.myname)
- class MyTest: myname = 'peter' def sayhello(self): print "say hello to %s"
% self.myname if __name__ == "__main__": MyTest.myname = 'hone' MyTest.sayhello
= lambda self,name: "I want say hello to %s" % name MyTest.saygoodbye =
lambda self,name: "I do not want say goodbye to %s" % name print MyTest().
sayhello(MyTest.myname) print MyTest().saygoodbye(MyTest.myname)
The variables and function definitions in the MyTest class are modified here, And the instantiated instance has different behavior characteristics.
Python application Tips 3. decorator
Decorator is a function that receives a function as a parameter and returns a function.
Code 3
- def enhanced(meth):
- def new(self, y):
- print "I am enhanced"
- return meth(self, y)
- return new
- class C:
- def bar(self, x):
- print "some method says:", x
- bar = enhanced(bar)
- def enhanced(meth): def new(self, y): print "I am enhanced"
return meth(self, y) return new class C: def bar(self, x):
print "some method says:", x bar = enhanced(bar)
The above is a typical application
Take the commonly used @ classmethod as an Example
The normal method is
Code 4
- class C:
- @classmethod
- def foo(cls, y):
- print "classmethod", cls, y
- class C: @classmethod def foo(cls, y): print "classmethod", cls, y
The above is what we will introduce to you about the Python application skills.