Python object-oriented static methods, class methods, property methods

Source: Internet
Author: User

static methods (Staticmethod nominal collation management, in which static properties in a class or instance are not actually accessed in a static method)
1 classDays (object):2     def __init__(self, food):3Self.food = Food4 5@staticmethod#actual and class are not related6     defTell (self):7         Print('there's%s,%s here!'% (Self.food,'name'))8 9 TenA = Days ('Banana') OneA.tell (a)
class method (Classmethod can only access class variables and cannot access instance variables)
1 lass F2 (object):2Name ='Big Cellular'  #class Variables3 4     def __init__(self, name):5Self.name =name6 7 @classmethod8     defEat (self):9         Print('There is%s, "%s"'% (Self.name,'methods of the class'))Ten  One  AF2.eat ()
attribute method (turns a method into a static property)
The definition and invocation of a property is a few points to note:1 when defining, add a @property adorner on the basis of a common method;2 when the property has only one self parameter , 3 is called. There are two ways to define a property without parentheses:1 Adorner: applying adorner 2 to a method static field: Defining a static field in a class with a value
Classic class, with a @property adorner:
1 classF3 (object):2     def __init__(self, name):3Self.name =name4Self.__food=None5 6 @property7     defeat2 (self):8         Print('here%s, "%s"'% (Self.name,'What's going on'))9 Ten  Onec = F3 ('Ah, pig .') AC.eat2
The new class, with three kinds of @property decorators, corresponds to three @property, @ method name. Setter, @ Method name. Deleter the method to be modified.
1 classF4 (object):2     def __init__(self, name):3Self.name =name4Self.__food=None5 6@property#turn a normal method into a property method to get7     defEat (self):8         Print('here%s, "%s"'% (Self.name, self.__food))#since there is no parameter Self.__food in this call9 Ten@eat. Setter#Modify One     defeat (self, food): A         Print('I want to eat%s'%Food ) -Self.__food= Food -  the@eat. Getter#Delete -     defEat (self): -         delSelf.__food -         Print('Delete field Self.__food') +  -  +D = F4 ('Ho') A d.eat atD.eat ='dumplings' - deld.eat -D.eat

Python object-oriented static methods, class methods, property methods

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.