For class (ii) static property, class, static methods

A.'S static property

That is the function attribute class, encapsulated into similar data attributes. For example: class method returns the value acquisition function. By: @proporty

class the Who (Object): 
    What = ' What ' 
    # Brother = 'Jack' 
    DEF  the __init__ (Self, name, Age, Gender): 
        the self.name = name 
        self.age = Age 
        self.gender = Gender
     DEF Information (Self):
         Print (the self.name, self.age, self.gender)
         return the self.name ## will add a return value of the function
 P1 = the Who ( ' John Doe ' , 18, ' M ' )
p1.information () function call to the class of the instance ##, there is (), because the properties of the dictionary class is stored in the memory address in the function, calls need to add ()
print(Who.information)    ##<function Who.information at 0x000002985CFFAA60>

The result: Joe Smith 18 M

() + None Return Value: Static attributes may be omitted (), return values ​​can be obtained.

##静态属性
class Who(object):
    what = 'what'
    # brother = 'jack'
    def __init__(self,name,age,gender):
        self.name = name
        self.age = age
        self.gender =gender
    @property
    def information(self):
        print(self.name,self.age,self.gender)
        return self.name
p1 = Who('张三',18,'')
a =p1.information     ## as a static property call, rather than a function call no [()]
Print(A)

Joe Smith18Male             
Joe Smith returns the value ##
print(Who.information)    ##<property object at 0x0000018E723967C8>

 

 Method 2 class

The method of the decorative @classmethod class, class attributes can be accessed.

2.1 instantiated by calling the class attributes: instantiating attribute data dictionary, you do not find what variables to find a higher level. So, by instantiating can easily call the class attribute what

class Who(object):
    what = 'what'
    # brother = 'jack'
    def __init__(self,name,age,gender):
        self.name = name
        self.age = age
        self.gender =gender
    def information(self):
        print(self.name,self.age,self.gender)
        return self.name
    def leishuxing(self):
        print(self.what)

p1 = Who('张三',18,'')
a = p1.leishuxing()  ##what

2.2 by not instantiated, how to call class attributes? Directly through the class calls, or you can call to class property. [External class called class attributes]

print(Who.what)  ##what

2.3 by not instantiating, internal call the class attribute in class And how does it work?

class Who(object):
    what = 'what'
    # brother = 'jack'
    def __init__(self,name,age,gender):
        self.name = name
        self.age = age
        self.gender =gender
    def information(self):
        print(self.name,self.age,self.gender)
        return self.name
    def leishuxing(self):
        print(self.what)
Who.leishuxing()

TypeError: leishuxing() missing 1 required positional argument: 'self'

Error message is displayed: by not instantiated class calls internal class attribute, a position parameter is missing self.

 @Classmethod by class methods, internal call class attributes of the class

class Who(object):
    what = 'what'
    # brother = 'jack'
    def __init__(self,name,age,gender):
        self.name = name
        self.age = age
        self.gender =gender
    def information(self):
        print(self.name,self.age,self.gender)
        return self.name
    @classmethod
    def leishuxing(self):
        print(self.what)
Who.leishuxing()  ##what

III. Static methods

Name only static methods attributive management, can not use the class variable is based toolkit (equivalent to an ordinary function). That is, this method function in class, regardless of the data attributes of the class, the class is merely placed inside a normal function

class Who(object):
    what = 'what'
    # brother = 'jack'
    def __init__(self,name,age,gender):
        self.name = name
        self.age = age
        self.gender =gender
    def information(self):
        print(self.name,self.age,self.gender)
        return self.name
    @classmethod
    def leishuxing(self):
        print(self.what)

    @staticmethod
    def state_fun(x):
        print('this is a family of %s'%x)
Who.state_fun('three')

this is a family of three

 

Guess you like

Origin www.cnblogs.com/liuhuacai/p/12596220.html