Python Day 33rd--static methods, class methods, property methods

Source: Internet
Author: User
After @staticmethod decoration, the methods in the class are converted to static methods

1 class A: 2     3      @staticmethod 4      def B (self): 5         Print ('')

A static method cannot access an instance variable or a class variable, which is equivalent to a toolkit in a class. such as OS, System and other import modules are generally

@classmethod decorated, the methods in the class are turned into class methods, the difference between class methods and ordinary methods is that class methods can only access class variables and cannot access instance variables

1  class   B (object):  2  name="  aa   " 3   @classmethod  4  def   CC (self):  5  print  ( '  %s .....   "%name)  6  7  8   9  b.cc () 

@property decorate, turn the method methods in the class into static properties

How to use static properties

1 classEat_food (object):2Self.__food=none#set a private property3     4 @property5     defEat (self):6         Print('. ...%s'%self.__food)7     8@eat. Setter#You can assign a value to it once you decorate it again9     defEat (Self,food):Ten         Print('. ...%s'%Food ) OneSelf.__food=food#Save to private property, alternate A      - @eat. Deleter -     defEat (self): the         delSelf.__food#Deleting a private property that holds an assignment is equivalent to deleting the property method

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.