Python-Class

来源:互联网 发布:中电41所 知乎 编辑:程序博客网 时间:2024/05/16 23:57

    • __init__ and __new__
    • Class Attribute
      • Access to Class Attributes
    • Binding and Method Invocation
    • Static Methods and Class Methods

__init__() and __new__()

When the class is invoked, the first step in the instantiation process is to create the instance object. Once the object is available, Python checks if an __init__() method has been implemented. By default, no special actions are enacted on the instance without the definition of (or overriding) of the special method __init__(). Any special action desired requires the programmer to implement __init__(), overriding its default behavior. If __init__() has not been implemented, the object is then returned and the instantiation process is complete.
However, if __init__() has been implemented, then that special method is invoked and the instance object passed in as the first argument (self), just like a standard method call. Any arguments passed to the class invocation call are passed on to __init__().
The __new__() special method bears a much closer resemblance to a real constructor than __init__(). The interpreter calls __new__(), a static method, with the class and passing in the arguments made in the class instantiation call. It is the responsibility of __new__() to call a superclass __new__() to create the object (delegating upward). __new__() has to return a valid instance so that the interpreter can then call __init__() with that instance as self. Calling a super class __new__() to create the object is just like using a new keyword to create an object in other languages.
Use __new__() when you need to control the creation of a new instance. Use __init__() when you need to control initialization of a new instance.
__new__() is the first step of instance creation. It’s called first, and is responsible for returning a new instance of your class. In contrast, __init__() doesn’t return anything; it’s only responsible for initializing the instance after it’s been created.
In general, you shouldn’t need to override __new__() unless you’re subclassing an immutable type like str, int, unicode or tuple.

Class Attribute

Instance have only data attributes (methods are strictly class attributes). Being able to create an instance attribute “on-the-fly” is one of the great features of Python classes.

Access to Class Attributes

Class attributes are simply data values associated with a class and not any particular instances. Class attributes can be accessed via a class or an instance.

>>> class C(object):...     version = 1.2... >>> c = C() # create a instance of class "C".>>> C.version # access to the class attribute via class.1.2>>> c.version # access to the class attribute via instance.1.2>>> C.version += 0.1 # change class attribute via class>>> C.version1.3>>> c.version # the "version" attribute of instance "c" is also changed.1.3

现在看来,貌似类中的属性和实例中的属性绑定在了一起,不禁让人怀疑所有这个类的实例中的该属性都具有相同数值,那么如何实现实例间的差异呢?通过下面的代码片段来看。

>>> class C(object):...     version = 1.2... >>> c = C()>>> c.version += 1 # change "version" attribute via instance "c".>>> c.version2.2>>> C.version # access class attribute "version" and find it is not changed.1.2>>> C.version += 0.1 # change class attribute "version".>>> C.version1.3>>> c.version # this time, the attribute "version" of instance "c" does not change with the class attribute.2.2

从1-9行可以看出对实例中的属性进行改动后,类属性不受影响。这说明实例中属性的变动不影响类属性。然后从10-14行可以看出,此时类属性的改变同样不能引起实例中相应属性的改变。从这个代码片段来看,仿佛类属性与实例属性没有绑定在一起,与第一个代码的结果完全相反。为什么会出现完全相反的结果呢?来看下面的代码:

>>> c = C()>>> c.version1.2>>> C.version1.2>>> C.version += 0.1>>> C.version1.3>>> c.version # at this moment, the instance and the class still seem binding together.1.3>>> c.version += 1 # now, change the instance attribute.>>> c.version2.3>>> C.version # here, the class and the instance are unbound anymore.1.3>>> c_2 = C()>>> c_2.version1.3>>> C.version += 0.1>>> C.version1.4>>> c_2.version1.4

在这段代码的1-10行,类和实例的属性仍绑定在一起,直到第11行,对实例属性重新赋值后发现,它们两个不再绑定在一起。可以得出结论,在对实例的属性进行第一次赋值后,类属性和实例属性不再绑定在一起。而且,从16-18行可以看出,新生成的实例与类属性保持一致。

Binding and Method Invocation

Methods can be called only when there is an instance of the class upon which the method was invoked. When there is an instance present, the method is considered bound (to that instance). Without an instance, a method is considered unbound. The first argument in any method definition is the variable self, which represents the instance object invoking the method. The main use case for calling a method belonging to a class that you do not have an instance for is the case where you are deriving a child class and overriding a parent method where you need to call the parent’s constructor you are overriding.

Static Methods and Class Methods

@classmethod means: when this method is called, we pass the class as the first argument instead of the instance of that class (as we normally do with methods). This means you can use the class and its properties inside that method rather than a particular instance.
@staticmethod means: when this method is called, we don’t pass an instance of the class to it (as we normally do with methods). This means you can put a function inside a class but you can’t access the instance of that class (this is useful when your method does not use the instance).
For class methods, instead of the instance, the class is required as the first argument, and it is passed in to the method by the interpreter. The class does not need to be specially named like self, but most people use cls as the variable name.

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手臂突然筋扭了怎么办 胳膊扭了肿了怎么办 胖人走路磨大腿怎么办 脖子上长了个淋巴结怎么办 面部危险三角区长痘痘怎么办 儿童脖子上有淋巴结节怎么办 左侧颈根部淋巴结肿大怎么办 人的三角区肿了怎么办 刮三角区肿了怎么办 乳腺增生引起的腋窝淋巴结怎么办 右边脸比左边脸大怎么办 六个月宝宝脖子有点歪怎么办 大人的头偏了怎么办 宝宝脖子睡偏了怎么办 宝宝头歪向左边怎么办 一岁宝宝头歪怎么办 宝宝头往右边歪怎么办 八个月宝宝头歪怎么办 宝宝头往左边偏怎么办 11月婴儿歪脖子怎么办 婴儿头往左边偏怎么办 宝宝头网的高怎么办 宝宝头歪向一边怎么办 2岁宝宝头睡偏了怎么办 6岁儿童头有点歪怎么办 宝宝脖子有点偏左边歪怎么办 宝宝脖子偏了怎么办呢 斜颈导致的脸歪怎么办 斜颈手术后脸部还不对称怎么办 宝宝3个月斜颈怎么办 一岁八个月宝宝斜颈怎么办 四个月宝宝有点斜颈怎么办 一岁宝宝有点偏怎么办 6个月宝宝有点斜颈怎么办 四个月宝宝左侧胸锁乳突肌厚怎么办 脖子疼好几天了怎么办 有双下巴怎么办才能瘦掉 胃突然疼的厉害怎么办 手劳损痛的厉害怎么办 手臂扭到了很疼怎么办 寒湿导致肩膀痛怎么办