About python special method new

迷茫
Release: 2017-03-25 14:33:22
Original
1655 people have browsed it

new(cls[, ...])

Called to create a new instance of class cls. new() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of new() should be the new object instance (usually an instance of cls).

Typical implementations create a new instance of the class by invoking the superclass’s new() method using <span class="pre">super(currentclass, <span class="pre">cls).new(cls[, <span class="pre">...])</span></span></span> with appropriate arguments and then modifying the newly-created instance as necessary before returning it.

If new() returns an instance of cls, then the new instance’s init() method will be invoked like <span class="pre">init(self[, <span class="pre">...])</span></span>, where self is the new instance and the remaining arguments are the same as were passed to new().

If new() does not return an instance of cls, then the new instance’s init() method will not be invoked.

new() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation.

调用产生一个新的类的实例,clsnew()是一个<a href="http://www.php.cn/wiki/188.html" target="_blank">静态</a>方法(不需要声明),类本身(cls)作为第一个参数,其他的的参数是传递给<a href="http://www.php.cn/wiki/60.html" target="_blank">对象</a><a href="http://www.php.cn/wiki/176.html" target="_blank">构造函数</a>的<a href="http://www.php.cn/wiki/81.html" target="_blank">表达式</a>(对类的调用),new()的返回值应该是一个新的对象实例(一般是cls的实例)。典型的实现方法就是在返回新生成的实例之前,调用父类的new()方法(super(currentclass, <span class="pre">cls).new(cls[, <span class="pre">...])</span></span>)来改变这个实例对象,比如说可以把实例里面字符的空格去掉等等(这句是我自己加的)。

如果new()返回了一个cls的实例对象,然后就会调用这个新的实例的init()方法(init[,...]),self指新创建的实例其余的参数和传递给new()的一样。

如果new()没有成功返回一个cls的实例,就不会调用这个实例的init()方法。

new()主要用来进行不可变类型(像是int,str,或者元组)的子类自定义实例的创建。也可以重写自定义元类来进行自定义类的创建。

举例:在实例化对象之前,先将字符串做一个处理,就可以用new,下面的例子就是做一个去空格处理。

class Word(str):

    def __new__(cls,word):

        if &#39; &#39; in word:
            print("there is qutos")
            word = &#39;&#39;.join(word.split())
        return str.__new__(cls,word)
        
a = Word(&#39;hello sherry&#39;)
print(a)
Copy after login

The above is the detailed content of About python special method new. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template