Operator overloading usage analysis in Python

Source: Internet
Author: User
This example describes the use of operator overloading in Python. Share to everyone for your reference, as follows:

Class can overload Python's operators

Operator overloading makes our objects the same as built-in. The method of __x__ 's name is a special hook, and Python intercepts the operator with this special name to implement overloading. Python automatically calls such a method when it calculates an operator, for example:

If the object inherits the __add__ method, this method is called when it appears in the + expression. By overloading, user-defined objects are like built-in.

Overloading operators in a class

1. Operator overloading allows the class to intercept standard Python operations.
2. Classes can overload all Python's expression operators.
3. Classes can overload object operations: Print, function call, qualification, etc.
4. Overloading makes instances of classes look more like built-in.
5, overloading is implemented by a special named class method.

Operation Description of method name overloading call expression
__init__ constructor Creation object: Class ()
__del__ when the destructor releases the object
__add__ "+" x + y
__or__ "|" x | Y
__repr__ print, convert print x, ' x '
__call__ function call X ()
__getattr__ Property Reference x.undefined
__getitem__ index x[key],for loop, in test
__setitem__ index Assignment X[key] = value
__getslice__ Shard X[low:high]
__len__ length len (x)
__cmp__ comparison x = = y, x < y
__radd__ operator "+" non-instance + X on right

Example:

__GETITEM__ intercepts all index operations

>>> class Indexer:def __getitem__ (self,index): Return index * * 2>>> x = indexer () >>> for i in RA Nge (5):p rint x[i]  #x [i] will call __getitem__ (x,i) 014916

Read more about Python topics: python Regular expression Usage summary, Python data structure and algorithm tutorials, Python socket Programming Tips Summary, Python function Usage tips summary, "How-to" Python string manipulation Tips Summary, Python Introductory and Advanced classic tutorials, and Python file and directory operations tips

I hope this article is helpful for Python program design.

  • 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.