Python-all special methods, magic methods, hooks

Source: Internet
Author: User
Tags bitwise

C.__init__ (self[, arg1, ...]) constructor (with some optional parameters)
C.__new__ (self[, arg1, ...]) the constructor (with some optional parameters) is typically used in subclasses that set the invariant data type.
C.__del__ (self) destructor
c.__str__ (self) printable character output, built-in STR (), and print statements
C.__repr__ (self) runtime string output built-in repr () and ' operator
c.__unicode__ (self) Unicode string output; built-in Unicode ()
C.__call__ (self, *args) represents a callable instance
C.__nonzero__ (self) defines the false value for object built-in bool () (starting with version 2.2)
C.__len__ (self) "Length" (Available for Class) built in Len ()


C.__cmp__ (self, obj) object comparison; built-in CMP ()
C.__lt__ (self, obj) less than/less than or equal to the corresponding < and <= operators
C.__gt__ (self, obj) greater than/greater than or equal to the corresponding > and >= operators
C.__eq__ (self, obj) equals/does not equal corresponding ==,!= and <> operator


C.__getattr__ (self, attr) Gets the property built-in GetAttr () called only when the property is not found
C.__setattr__ (self, attr, Val) setting properties
C.__delattr__ (self, attr) Delete property
C.__getattribute__ (self, attr) a gets the property; built-in getattr () is always called
C.__get__ (self, attr) A (descriptor) Get property
C.__set__ (self, attr, Val) A (Descriptor) setting property
C.__delete__ (self, attr) A (descriptor) Delete property


C.__*add__ (self, obj) plus; + operator
C.__*sub__ (self, obj) minus;-operator
C.__*mul__ (self, obj) by; * operator
C.__*div__ (self, obj) except;/operator
C.__*truediv__ (self, obj) e True except;/operator
C.__*floordiv__ (self, obj) e floor except;//Operator
C.__*mod__ (self, obj) modulo/take redundancy;% operator
C.__*divmod__ (self, obj) except and modulo; built-in Divmod ()
C.__*pow__ (self, obj[, mod]), built-in POW (); * * operator
C.__*lshift__ (self, obj) left shift;<< operator


C.__*rshift__ (self, obj) Move right;>> operator
C.__*and__ (self, obj) bitwise AND;& operators
C.__*or__ (self, obj) bitwise OR; | operator
C.__*xor__ (self, obj) bitwise and OR; ^ operator


C.__neg__ (self) One dollar negative
C.__pos__ (self) is a dollar
C.__abs__ (self) absolute value; built-in ABS ()
C.__INVERT__ (self) bitwise negation; ~ operator


c.__complex__ (self, COM) to complex (plural); built-in complex ()
C.__int__ (self) to int; built-in int ()
C.__long__ (self) to long; built-in long ()
C.__float__ (self) to float; built-in float ()


C.__oct__ (self) octal representation; built-in Oct ()
c.__hex__ (self) hexadecimal representation; built-in Hex ()


C.__COERCE__ (self, num) is compressed into the same numeric type; built-in coerce ()
C.__index__ (self) g when necessary, the compression optional numeric type is integer (for example: for slicing



The number of items in the c.__len__ (self) sequence
C.__GETITEM__ (self, Ind) gets a single sequence element
C.__setitem__ (self, Ind,val) sets a single sequence element
C.__DELITEM__ (self, IND) delete a single sequence element


C.__getslice__ (self, ind1,ind2) get sequence Fragment
C.__setslice__ (self, i1, i2,val) set sequence fragment
C.__delslice__ (self, ind1,ind2) Delete sequence fragment
C.__contains__ (self, val) f test sequence member; built in keyword
c.__*add__ (self,obj) concatenation; + operator
c.__*mul__ (self,obj) repetition; * operator
C.__ITER__ (self) e creates an iterative class; built-in ITER ()


The number of items in the c.__len__ (self) mapping
C.__hash__ (self) hash (hash) function value
c.__getitem__ (Self,key) Gets the value of the given key (key)
C.__SETITEM__ (self,key,val) Sets the value of the given key (key)
c.__delitem__ (Self,key) Delete the value of the given key (key)
C.__MISSING__ (Self,key) Given a key if it does not exist in the dictionary, provide a default value

Python-all special methods, magic methods, hooks

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.