New details of Python's special methods

Source: Internet
Author: User
object.__new__ (cls[, ...])

Called to create a new instance of class CLS. __new__ () is 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 Super (Curre Ntclass, CLS). __new__ (cls[, ...]) with appropriate arguments and then modifying the newly-created instance as necessary be Fore returning it.

If __new__ () returns an instance of CLS, then the new instance ' s __init__ () method would be a invoked like __init__ (self[,.. .]), where the new instance and the remaining arguments are the same as were passed to __new__ ().

If __new__ () does not return a instance of CLS, then the new instance ' s __init__ () method won't be invoked.

__NEW__ () is intended mainly to allow subclasses of immutable types (like Int., STR, or tuple) to customize instance Creati On. 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__ () is a static method (which does not need to be declared), the class itself (CLS) is the first parameter, the other arguments are the expressions passed to the object constructor (calls to the Class), and the return value of __new () __ should be a new object instance (typically a CLS instance). A typical implementation is to call the parent class's __new () __ Method (Super (Currentclass, CLS)) before returning the newly generated instance. __new__ (cls[, ...]) ) To change this instance object, for example, you can remove the characters in the instance space and so on (this sentence is my own addition).

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.

Class Word (str):    def __new__ (cls,word):        if ' in Word:            print ("There is Qutos")            Word = '. Join ( Word.split ())        return str.__new__ (cls,word)        a = Word (' Hello Sherry ') print (a)


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.