Python Python Magic Method (pending pits)

Source: Internet
Author: User

Reference post: http://pyzh.readthedocs.io/en/latest/python-magic-methods-guide.html

Reference Blog English Original: http://www.rafekettler.com/magicmethods.html

Construction Method:

1, __new__: The first call, remove the CLS parameter, and pass all other parameters to __init__, only applicable to the inheritance unchanged analogy such as int, tuple,string.

2. __init__: Initialization method

3. __DEL__: Destroy the object, for example, when the object is destroyed by closing the open file.

Comparison operators:

1, __cmp__: If all the comparison standards are unified, can be achieved through this implementation, otherwise you have to implement other comparison operators in turn. (Self>other returns 1)

2, __eq__, __ne__, __lt__, __gt__, __le__, __ge__: =,! =, <,;, <=, >=

3, Functools has a class decorator, as long as the definition of __eq__ and another operator, you can help to implement the comparison method, @total_ordering.

Numeric operator: unary

1, __pos__: Take positive

2, __neg__: Take negative

3. __abs__: Absolute Value

4, __invert__: Take the inverse operator ~

5, __round__: approximate number of digits

6. __floor__: Rounding Down

7, __ceil__: Rounding up

8, __trunc__: Take the distance 0 nearest integer

Number operator: two Yuan

1, __add__

2, __sub__

3, __mul__

4, __floordiv__://

5, __div__:/

6, __mod__

7, __divmod__

Python Python Magic Method (pending pits)

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.