实例方法、类数据属性(类变量)和实例数据属性(实例变量)、类方法@classmethod、静态方法@staticmethod及静态属性@property初识


1、实例方法、类数据属性(类变量)和实例数据属性(实例变量):

【python--实例属性、实例变量与类属性、类变量_hubing的博客-CSDN博客 https://blog.csdn.net/hubingshabi/article/details/101307448】

【Python类变量和实例变量(类属性和实例属性) http://c.biancheng.net/view/2283.html】

这2篇博客将实例属性、实例变量与类属性、类变量说的比较清楚。

实例方法:

定义:第一个参数必须是实例对象,该参数名一般约定为“self”,通过它来传递实例的属性和方法(也可以传类的属性和方法); 调用:只能由实例对象调用。

类数据属性(类变量):

类变量指的是在类中,但在各个类方法外定义的变量。类变量的特点是,所有类的实例化对象都同时共享类变量,也就是说,类变量在所有实例化对象中是作为公用资源存在的。注意,因为类变量为所有实例化对象共有,通过类名修改类变量的值,会影响所有的实例化对象。

实例数据属性(实例变量):指的是在任意类方法内部,以“self.变量名”的方式定义的变量,其特点是只作用于调用方法的对象。另外,实例变量只能通过对象名访问,无法通过类名访问。

2、类方法@classmethod:

文档原文:


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

class C: @classmethod def f(cls, arg1, arg2, ...): ... The @classmethod form is a function decorator – see Function definitions for details.

A class method can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument.


定义:使用装饰器@classmethod。第一个参数必须是当前类对象,该参数名一般约定为“cls”,通过它来传递类的属性和方法(不能传实例的属性和方法); 调用:实例对象和类对象都可以调用,通过类名不仅可以调用类变量,也可以修改它的值。因为类变量为所有实例化对象共有,通过类名修改类变量的值,会影响所有的实例化对象。

应用:类方法是将类本身作为对象进行操作的方法。假设有个方法,且这个方法在逻辑上采用类本身作为对象来调用更合理,那么这个方法就可以定义为类方法。另外,如果需要继承,也可以定义为类方法。

3、静态方法@staticmethod

文档原文:


@staticmethod

Transform a method into a static method.

A static method does not receive an implicit first argument. To declare a static method, use this idiom:

class C: @staticmethod def f(arg1, arg2, ...): ... The @staticmethod form is a function decorator – see Function definitions for details.

A static method can be called either on the class (such as C.f()) or on an instance (such as C().f()).

Static methods in Python are similar to those found in Java or C++. Also see classmethod() for a variant that is useful for creating alternate class constructors.

Like all decorators, it is also possible to call staticmethod as a regular function and do something with its result. This is needed in some cases where you need a reference to a function from a class body and you want to avoid the automatic transformation to instance method.


定义:使用装饰器@staticmethod。参数随意,没有“self”和“cls”参数,但是方法体中不能使用类或实例的任何属性和方法,不能使用类变量和实例变量,不能访问类属性和实例属性;

调用:实例对象和类对象都可以调用。

应用:静态方法是类中的函数,不需要实例。静态方法主要是用来存放逻辑性的代码,逻辑上属于类,但是和类本身没有关系,也就是说在静态方法中,不会涉及到类中的属性和方法的操作,一般来说可以存放此类的说明或描述。可以理解为,静态方法是个独立的、单纯的函数,它仅仅托管于某个类的名称空间中,便于使用和维护。

4、静态属性@property

文档原文:


1 class property(fget=None, fset=None, fdel=None, doc=None) 2 Return a property attribute. 3 fget is a function for getting an attribute value. fset is a function for setting an attribute value. fdel is a function for deleting an attribute value. And doc creates a docstring for the attribute. 4 A typical use is to define a managed attribute x: 5 6 class C: 7 def init(self): 8 self._x = None 9 10 def getx(self): 11 return self._x 12 13 def setx(self, value): 14 self._x = value 15 16 def delx(self): 17 del self._x 18 19 x = property(getx, setx, delx, "I'm the 'x' property.") 20 If c is an instance of C, c.x will invoke the getter, c.x = value will invoke the setter and del c.x the deleter. 21 If given, doc will be the docstring of the property attribute. Otherwise, the property will copy fget’s docstring (if it exists). This makes it possible to create read-only properties easily using property() as a decorator: 22 23 class Parrot: 24 def init(self): 25 self._voltage = 100000 26 27 @property 28 def voltage(self): 29 """Get the current voltage.""" 30 return self._voltage 31 The @property decorator turns the voltage() method into a “getter” for a read-only attribute with the same name, and it sets the docstring for voltage to “Get the current voltage.” 32 A property object has getter, setter, and deleter methods usable as decorators that create a copy of the property with the corresponding accessor function set to the decorated function. This is best explained with an example: 33 34 class C: 35 def init(self): 36 self._x = None 37 38 @property #在x函数前加上@property,使得该函数可直接调用
39 def x(self): 40 """I'm the 'x' property.""" 41 return self._x 42 43 @x.setter 44 def x(self, value): 45 self._x = value 46 47 @x.deleter 48 def x(self): 49 del self._x 50 This code is exactly equivalent to the first example. Be sure to give the additional functions the same name as the original property (x in this case.) 51 The returned property object also has the attributes fget, fset, and fdel corresponding to the constructor arguments.


定义:作用就是把类的函数属性,封装成类似数据属性。将类的函数通过@property属性封装,封装后实例调用该函数时,不再需要在函数后面加(),而是用类似调用数据属性的方式直接调用函数名称即可执行函数。相当于把函数封装成数据属性的形式,使外部在调用的时候,看不见内部的逻辑,实例可以访问到实例的数据属性,可以访问类的数据属性和函数属性。传的参数为self。

静态属性既可以访问类的属性,也可以访问实例的属性。


统一声明:关于原创博客内容,可能会有部分内容参考自互联网,如有原创链接会声明引用;如找不到原创链接,在此声明如有侵权请联系删除哈。关于转载博客,如有原创链接会声明;如找不到原创链接,如果有哪些问题、有哪些不妥或者侵犯到您的权益的地方,可以联系我,我马上修改。