I. About special attributes (property).
Then the previous article went on to say that if you want to invoke the properties of an object in Python, use the class or object name directly behind it. Click and then write the attribute name to skip the class or object's properties, for example, as below.
Class Person:
def __init__ (Self,career):
Self.career = career
Ayumi = person (career= "diva")
Print Ayumi.career #ayumi是实例化出来的一个对象后面加. Point, followed by the property name, is called directly to the career property of this object.
What it says is to invoke the properties of an object, what exactly is a particular property?
The so-called special attribute is to execute a function in an object in a way that accesses the object's properties.
As an example:
Class Person:
def __init__ (Self,career):
Self.career = career
@property
def show_carrer (self):
Return Self.career
Ayumi = person ("diva")
The print Ayumi.show_carrer #show_carrrer itself is a function that needs to be followed by parentheses to execute, but the function is now executed using the method that invokes the property of the object.
# to explain, @property is a python built-in decorator that is "decorated" by this adorner function (method), when the object to call, directly in the way the property is called to execute the function.
Two. About the class method (Classmethod).
The class method is to define a method in the class that calls the method without generating any objects, and the function that is called directly through the class is the class method.
Let's take an example ~:
Class Num:
Count = 0
def __init__ (self):
Num.count + = 1
def __del__ (self):
Num.count-= 1
@classmethod
def show_count (CLS): "---ATTENTION!! Here the incoming is no longer an object, but a class!!
Return Num.count
A = num ()
b = num ()
Print Num.show_count ()
# @classmethod is also a python built-in adorner that transforms a function in a class into a function that the class can call directly, without instantiating any objects.
Three. About static methods (Staticmethod).
What is a static method?
A static method is a normal function defined within a class, but this function does not have any binding relationship with the class and object, the function cannot access any of the properties in the object, nor can it access any property in the class, but this function is either called by the class, or it can be called through the object, which is placed inside the class. But functions that have nothing to do with classes are static methods.
Import time
Class Num:
Count = 0
def __init__ (self):
Num.count + = 1
def __del__ (self):
Num.count-= 1
@classmethod
def show_count (self):
Return Num.count
@staticmethod #定义静态方法
Def dis_time ():
Return Time.localtime ()
A = num ()
b = num ()
Print b.dis_time () # calls a static method from an object.
>>>time.struct_time (tm_year=2017, tm_mon=4, tm_mday=2, tm_hour=16, tm_min=17, tm_sec=36, tm_wday=6, tm_yday= , tm_isdst=0)
Print num.dis_time () # calls a static method through a class.
>>>time.struct_time (tm_year=2017, tm_mon=4, tm_mday=2, tm_hour=16, tm_min=17, tm_sec=36, tm_wday=6, tm_yday= , tm_isdst=0)
As you can see from the above example, a static method only nominally belongs to a class or object, but there is no way to access any of the class properties and instance properties.
This article is from the "Rebirth" blog, make sure to keep this source http://suhaozhi.blog.51cto.com/7272298/1912572
8.python Face Object Part.2 (special attribute, class method, static method)