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 asself
. Calling a super class__new__()
to create the object is just like using anew
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 likeself
, but most people usecls
as the variable name.
- python class
- Python Class
- Python Class
- Python - class
- Python class
- python Class
- python-class
- Python Class
- <python>class
- Python-Class
- python object and class
- python.struct helper class
- python Object And Class
- Python[十四]:Class
- 菜鸟学 Python-Class
- python class属性
- python class 用法
- python class 的属性
- ubuntu14.04上用vsftpd建立匿名ftp服务器
- 【JavaScript】(4)普通函数、动态函数定义格式,调用方式,注意的详细细节
- Object类
- Prime Time
- 开灯问题
- Python-Class
- 简谈反射
- 四月英语——你是人间四月天
- Linux 常用命令
- mysql---存储引擎
- HDOJ 2000 ASCII码排序
- 写给初学者29_android_安卓权限机制
- Android期末复习(3)-service
- Java的join()方法