Improve your Python code (8) __python

Source: Internet
Author: User
Tags instance method
1. [] {} (). Use list parser instead of for traversal

Reason:
University 2. A function pass is not a reference or a value

A = 5   # opens up a memory storage 5, put a tag on this memory
B = A # put   b This tag on that memory
B = 7   # Open a memory storage 7, paste B This tag on this memory, just that B is covered.


3. *args,**kwargs



4. Staticmethod and Classmethod

The first thing to understand is what is an instance method, a static method, and a class method:

Class Demo (object): Def instance_method (self, Your_para): "" "This is a instance_method you Should call it like the FOLLOW:A = Demo () a.instance_method (Your_para) plus:in python, we denot E ' CLS ' as latent para of class while ' self ' as latent para of the instance of the Class:p Aram Your_para :: Return: "" "Print (called Instance_method and get:, Your_para) @classmethod def class _method (CLS, Your_para): "" "" is a class_method can call it like the Follow:metho D1:a = Demo () a.class_method (Your_para) Method2:Demo.class_method plus:in PYT Hon, we denote ' cls ' as latent para of class while ' self ' as latent para of the instance of the class:p AR
    Am Your_para:: return: "" "Print (called Class_method and get:, Your_para) @staticmethod def static_method (your_Para): "" "" is a static_method and can call it like the methods of Class_method :p Aram Your_para:: Return: "" "Print (called Static_method and get:, Your_para)
Although the class method does not explicitly declare the CLS when invoked, the class itself is actually passed in as an implied parameter. This is like an instance method that does not explicitly declare self when invoked, but in practice the instance itself is passed in as an implied parameter. For static functions, we generally define a class-independent and instance-independent function as a static function. For example, the function of a portal check is best defined as a static function. The beauty of the class method:
1, in the role of inheritance:
Class Fruit (object): total = 0 # It is a class attribute @classmethod def print_total (CLS): Print (' This is the ', C LS, '. Total: ', cls.total, ' and its ID: ', ID (cls.total)] # The CLS is the class itself, printing the value of the total of the class attributes print (' This is the Fruit.total : ', Fruit.total, ' and its ID: ', ID (fruit.total)) print ("=======================") @classmethod def set (cl s, value): Cls.total = value class Apple (Fruit): Pass class Orange (Fruit): Pass App1 = Apple () app1.se   T (a) App1.print_total () apple.print_total () Fruit.set (2) app1.print_total () fruit.print_total () "" "Output:this is the <class ' __main__.
Apple ' > Total:10 and its id:1355201264 this is the fruit.total:0 and its id:1355200944 ======================= This is the <class ' __main__.
Apple ' > Total:10 and its id:1355201264 this is the fruit.total:0 and its id:1355200944 ======================= This is the <class ' __main__. Apple ' > Total:10 and its id:1355201264 this is theFruit.total:2 and its id:1355201008, ======================= this is the <class ' __main__. Fruit ' >. Total:2 and its id:1355201008 the ' it fruit.total:2 and its id:1355201008 ======================= '
 ""
Related Article

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.