Common Python application skills Content Analysis

Source: Internet
Author: User

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

 
 
  1. class MyTest:   
  2. myname = 'peter'   
  3. def sayhello(hello):   
  4. print "say hello to %s" % hello.myname   
  5. if __name__ == "__main__":   
  6. MyTest().sayhello()   
  7. 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

 
 
  1. class MyTest:   
  2. myname = 'peter'   
  3. def sayhello(self):   
  4. print "say hello to %s" % self.myname   
  5. if __name__ == "__main__":   
  6. MyTest.myname = 'hone'   
  7. MyTest.sayhello = lambda self,name: "I want say hello to %s" % name   
  8. MyTest.saygoodbye = lambda self,name: "I do not want say goodbye to %s" % name   
  9. print MyTest().sayhello(MyTest.myname)   
  10. print MyTest().saygoodbye(MyTest.myname)   
  11. 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

 
 
  1. def enhanced(meth):   
  2. def new(self, y):   
  3. print "I am enhanced"   
  4. return meth(self, y)   
  5. return new   
  6. class C:   
  7. def bar(self, x):   
  8. print "some method says:", x   
  9. bar = enhanced(bar)   
  10. 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

 
 
  1. class C:   
  2. @classmethod   
  3. def foo(cls, y):   
  4. print "classmethod", cls, y   
  5. 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.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.