[Little Turtle] Getting started learning Python Notes "Magic Method"

Source: Internet
Author: User

// __new__ (cls[,....]) // object instantiates the first method of the call, its first argument is this class, and the other parameters are passed directly to the __init__ () method // overriding the __new__ () method when you need to modify it on the basis of a base class

// __del__ () method // The __del__ () method is called only if all objects instantiated by this class are dropped by Del

//operator overloading in PythonclassNew_int (int)://subclass New_int Based on the base class intdef__add__ (Self,other)//overloaded operator +        return int. __sub__ (Self,other)//returns the subtraction operation of a base classclassNew_int (int): def__add__ (self,other):return int(self)-int(Other)//equivalent to int. __sub__ (self,other)>>>a = New_int (1)>>>b = New_int (3)

[Little Turtle] Getting started learning Python Notes "Magic Method"

Related Article

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.