New in Python's special method

Source: Internet
Author: User

object.__new__(cls[, ... .. ])

Called to create a new instance of class CLS. is __new__() a static method (special-cased so need not declare it as such) that takes the class of which an instance was Requested as its first argument. The remaining arguments is those passed to the object constructor expression (the call to the Class). The return value of __new__() should is 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 with super(currentclass, cls).__new__(cls[, ...]) appr Opriate 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 would be a invoked like __init__(self[, ...]) , where s Elf is the new instance and the remaining arguments be the same as were passed to __new__() .

If __new__() does not return a instance of CLS, then the new instance ' s method is not being __init__() 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 the custom metaclasses in order to customize class creation.

called to produce an instance of a new class, CLS__new__()是一个静态方法(不需要声明),类本身(cls)作为第一个参数,其他的的参数是传递给对象构造函数的表达式(对类的调用),__new()__的返回值应该是一个新的对象实例(一般是cls的实例)。典型的实现方法就是在返回新生成的实例之前,调用父类的__new()__方法(super(currentclass, cls).__new__(cls[, ...]))来改变这个实例对象,比如说可以把实例里面字符的空格去掉等等(这句是我自己加的)。

If __new () __ Returns a CLS instance object, then it calls the new instance's __init () __ Method (__init__[,...] ), self refers to the remaining parameters of the newly created instance and is passed to __new () as well.

If __new () __ does not successfully return an instance of the CLS, the Init () method of the instance is not invoked.

__new () __ is used primarily for the creation of subclass custom instances of immutable types (such as int,str, or tuples). You can also override custom meta-classes to create custom classes.

Example: Before instantiating the object, the string is processed first, you can use __new__, the following example is to do a whitespace processing.

classWord (str):def __new__(Cls,word):if ' ' inchWord:Print("there is Qutos") Word="'. Join (Word.split ())returnStr.__new__(Cls,word) a= Word ('Hello Sherry')Print(a)
View Code

New in Python's special method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.