Staticmethod Classmethod modifier

Source: Internet
Author: User

First, Staticmethod (function)

Return a static method for function.
A static method does not receive an implicit first argument. To declare a static method with this idiom:

class Sing ():    @staticmethod    def  SayHello ():        print"Hello World "    def  Sayno ():        print"no " if __name__ ' __main__ ' :    Sing.sayhello ()    sing.sayno (  )

Sing.sayno error is as follows:

Typeerror:unbound method Sayno () must is called with Sing instance as first argument (got nothing instead)
Sayno () method is not a static method, you need to instantiate the object sing before calling the method
and the SayHello () method does not error, because the Staticmethod modification
The following method is the right one:

 class   Sing (): @staticmethod  def   SayHello ():  print   " hello World  "  def   Sayno (self):  print   " no  "   __name__  = =  " __main__   " : Sing.sayhello () Sing  = Sing () Sing.sayno ()  

Second, Classmethod (function)

Return A class method for function.
A class method receives the class as implicit first argument, just like an instance method receives the instance. To declare a class method with this idiom:

 class   Sing (): @classmethod  def   SayHello (self):  print   " hello World  "  def   Sayno (self):  print   " no  "   __name__  = =  " __main__   " : Sing.sayhello () Sing  = Sing () Sing.sayno ()  

Note: The Staticmethod modified method does not need to be connected to the self parameter, while the method of Classmethod modification requires the self parameter;

Third, @property modifiers (the new class is unique, the new class is the class that the object defines)
The properties of the class are decorated as follows for the definition of the class without the @property modifier:

 class   C (object):  def   =< Span style= "color: #000000;" > None  def   Getx (self):  return   self._x  def   Setx (self, value): self._x  = value  d EF   del   self._x x  = Property (Getx, Setx, Delx,  " i ' M the ' X ' property.  ) 

Definition of a class decorated with the @property modifier: (Introduction of Setter,deleter,getter property starting from Python2.6)

class C (object):     def __init__ (self): self._x = None    @property    def  x (self):        "" "I ' m the ' x ' property. """        return self._x    @x.setter    def  x (Self, value):        = value    @ X.deleter    def  x (self):        del self._x

Staticmethod Classmethod modifier

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.