Special methods of classes in Python

Source: Internet
Author: User
 

Link: http://www.pythonclub.org/python-class/special-function

Generally, special methods are used to imitate a behavior. For example, if you want to use index operations such as x [key] for your class (just like lists and meta groups), you only need to implement the getitem () method. Think about it. Python does this for the list class!

The following table lists some useful special methods. If you want to know all the special methods, you can find a huge list in the Python reference manual.

========================================================== ==============================================

Description

---------------------------------------------------------

_ Init _ (self,...) is called before the newly created object is returned for use.

_ Del _ (self) is called just before the object is deleted.

_ Str _ (self) is called when we use the print statement for the object or str.

_ Lt _ (self, other) is called when the less than operator (<) is used. Similarly, all operators (+,>, and so on) have special methods.

_ Getitem _ (self, key) is called when the x [key] index operator is used. _ Len _ (self) is called when the built-in len () function is used for the sequence object.

_ Repr _ (self) repr () and '...' conversions

_ Cmp _ (self, o) Compares s to o and returns <0, 0, or> 0. Implements>, <, = etc...

_ Hash _ (self) is used to calculate the 32-bit hash value in set and dict.

_ Eq _ (self, other) is used to compare whether two objects are equal.

_ Nonzero _ (self) Returns 0 or 1 for truth value testing _ getattr _ (self, name) called when attr lookup doesn't find <name>

_ Setattr _ (self, name, val) called when setting an attr (inside, don't use "self. name = value "use" self. _ dict _ [name] = val ")

_ Delattr _ (self, name) called to delete attr <name >__ call _ (self, * args) called when an instance is called as function.

========================================================== ===================================================,

For set and dict, the two most important methods are _ hash _ (self) and _ eq _ (self, other)

These two methods are similar to the Object in Java. hashCode () and Object. the equals () method is similar. If you want to make two objects with the same content to be considered equal, you must reload these two functions, as shown below:

class Point:    x=-1    y=-1    def __init__(self,x,y):        self.x=x        self.y=y    def __hash__(self):        return hash(self.x)^hash(self.y)    def __eq__(self, other):        return isinstance(other, self.__class__) and self.x == other.x and self.y == other.y

To determine whether a Point type p is in the collection, the judgment will de-duplicate:

if p not in set:   #do something



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.