Python static properties, static methods, class methods

Source: Internet
Author: User

#静态属性的作用是吧类的方法隐藏起来 (can hide the logic behind it) so that the user feels that it is in the electrophoresis attribute, not the method class Static:attribute= ' Properties ' def __init__ (Self,parameter1, PARAMETER2,PARAMETER3): Self.parameter1 = parameter1 Self.parameter2 = parameter2 Self.parameter3 = P Arameter3 def test (self): return (' Test ', self.parameter1) @property def print_static (self): print (S Tatic.attribute) return (' This is the result of the static property%s%s '% (self.parameter1,self.parameter2,self.parameter3) ') s=static (' Hello ', ' World ', '! ') Print (S.test ()) print (s.print_static)

static method:

#静态方法只是名义上归类管理, cannot use class variables and instance variables, is the toolkit of the class # My personal understanding is that the tiger skin is a big flag, and the static method just borrows a separate function under the tree of the class static:attribute= ' Properties ' def __init__ (     Self,parameter): Self.parameter=parameter def Test (self): return (' Test ', Self.parameter) @staticmethod def print_static (a,b,c): print (Static.attribute) return (' This is a static method ', A+b+c) s=static (' Hello ') print (S.test () ) Print (Static.print_static (2,4,6))

Class method:

The @classmethod class method is to call the class itself's properties through a function inside the class ' Class class (): The properties of attribute= ' Classes #类的属性 def __init__ (self,parameter): #这里的self是一     Object Self.parameter=parameter #实例的属性 def test (self): print (self) return (' class method Test call ', Self.parameter) @classmethod # class methods can only call class properties, cannot invoke instance Properties def print_class (CLS): #这里的cls是一个类 print (CLS) return (' properties of the class called Through class ', CLS . Attribute) X=class (' attribute of instance ') print (X.test ()) print (x.parameter) print (Class.print_class ()) print (Class.attribute)


Python static properties, static methods, class methods

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.