Python 33rd days ---- static method, class method, attribute method, python 33rd days

Source: Internet
Author: User

Python 33rd days ---- static method, class method, attribute method, python 33rd days

@ Staticmethod after decoration, the methods in the class are converted to static methods

1 class a: 2 3 @ staticmethod4 def B (self): 5 print ('')

Static methods cannot access instance variables or class variables, which is equivalent to the toolkit in the class. For example, OS, system, and other import modules

 

@ Classmethod: After the decoration, the methods in the class are converted into class methods. The difference between class methods and common methods is that class methods can only be class variables, but cannot access instance variables.

1 class B (object): 2 name = 'A' 3 @ classmethod4 def cc (self): 5 print ('% s. l ..... '% name) 6 7 8 9 B. cc ()

@ Property: after decoration, convert the method methods in the class into static properties.

Static attributes

1 class Eat_food (object): 2 self. _ food = None # Set a private property 3 4 @ property 5 def eat (self): 6 print ('..... % s' % self. _ food) 7 8 @ eat. setter # assign 9 def eat (self, food): 10 print ('..... % s' % food) 11 self. _ food = food # Save it to the private attribute. Backup 12 13 @ eat. deleter14 def eat (self): 15 del self. _ food # Delete the private attribute that saves the assigned value, which is equivalent to deleting this attribute method.

 

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.