Special properties of modules
__doc__ Description of the module
__all__ Indicates the properties that the module wants to use 、 Classes and methods
__file__ Module path
Special properties of class
__slots__ Limit the properties that instances of a class are allowed to add , It's a tuple Tuples
Class
__new__(cls[, ...])

1. __new__ Is the first method called when an object is instantiated
2. Its first argument is this class , Other parameters are used to pass directly to __init__ Method
3. __new__ Decide whether to use the __init__ Method , because __new__ You can call the constructor of other classes or directly return other instance objects as instances of this class , If __new__ No instance object returned , be __init__ Will not be called
4. __new__ It is mainly used to inherit an immutable type such as a tuple perhaps string

__init__(self[, ...]) Constructors , Initialization method called when an instance is created
__del__(self) Destructor , Method called when an instance is destroyed
__call__(self[, args...]) Allow an instance of a class to be called like a function :x(a, b) call x.__call__(a, b)
__len__(self) Definition should be len() Behavior at call time
__repr__(self) Definition should be repr() Behavior at call time , It's for debugging . When print() Called when the
__str__(self) Definition should be str() Behavior at call time
__bytes__(self) Definition should be bytes() Behavior at call time
__hash__(self) Definition should be hash() Behavior at call time
__bool__(self) Definition should be bool() Behavior at call time , Should return to True or False
__format__(self, format_spec) Definition should be format() Behavior at call time
Attributes related to
__getattr__(self, name)  Defines the behavior when a user tries to get a nonexistent property  
__getattribute__(self, name)  Defines the behavior of the class when its properties are accessed  
__setattr__(self, name, value) Defines the behavior when a property is set  
__delattr__(self, name)  Defines the behavior when an attribute is deleted  
__dir__(self)  Define when dir() Behavior when called  
__get__(self, instance, owner)  Defines the behavior when the value of the descriptor is taken  
__set__(self, instance, value)  Defines the behavior of a descriptor when its value is changed  
__delete__(self, instance)  Defines the behavior when the descriptor's value is deleted  
Comparison operator
 __lt__(self, other) Define the behavior of the less than sign :x < y call x.__lt__(y) 
__le__(self, other)  Define behavior less than or equal to sign :x <= y call x.__le__(y) 
__eq__(self, other)  Define the behavior of the equals sign :x == y call x.__eq__(y) 
__ne__(self, other)  The act of defining inequality :x != y call x.__ne__(y) 
__gt__(self, other)  Define the behavior of the greater than sign :x > y call x.__gt__(y) 
__ge__(self, other)  Define behavior greater than or equal to sign :x >= y call x.__ge__(y) 
   Arithmetic operator
__add__(self, other)   The act of defining addition :+
__sub__(self, other)  Define the act of subtraction :- 
__mul__(self, other)  Define the behavior of multiplication :* 
__truediv__(self, other)    The act of defining division :/
__floordiv__(self, other) Define the behavior of integer division ://
__mod__(self, other) Define the behavior of the modulus algorithm :%
__divmod__(self, other) Definition should be divmod() Behavior at call time
__pow__(self, other[, modulo]) Definition should be power() Call or ** The act of calculating
__lshift__(self, other) Define the behavior of bitwise left shift :<<
__rshift__(self, other) Define the behavior of bitwise right shift :>>
__and__(self, other) Define the behavior of biting and manipulation :&
__xor__(self, other) Defines the behavior of bitwise exclusive or operations :^
__or__(self, other) To define the act of biting or manipulating :|
Inverse operation
__radd__(self, other) Same as arithmetic operators , Called when the left operand does not support the corresponding operation
__rsub__(self, other) Same as arithmetic operators , Called when the left operand does not support the corresponding operation
__rmul__(self, other) Same as arithmetic operators , Called when the left operand does not support the corresponding operation
__rtruediv__(self, other) Same as arithmetic operators , Called when the left operand does not support the corresponding operation
__rfloordiv__(self, other) Same as arithmetic operators , Called when the left operand does not support the corresponding operation
__rmod__(self, other)  Same as arithmetic operators , Called when the left operand does not support the corresponding operation  
__rdivmod__(self, other)  Same as arithmetic operators , Called when the left operand does not support the corresponding operation  
__rpow__(self, other)  Same as arithmetic operators , Called when the left operand does not support the corresponding operation  
__rlshift__(self, other)  Same as arithmetic operators , Called when the left operand does not support the corresponding operation  
__rrshift__(self, other)  Same as arithmetic operators , Called when the left operand does not support the corresponding operation  
__rand__(self, other)  Same as arithmetic operators , Called when the left operand does not support the corresponding operation  
__rxor__(self, other)  Same as arithmetic operators , Called when the left operand does not support the corresponding operation  
__ror__(self, other)  Same as arithmetic operators , Called when the left operand does not support the corresponding operation  
Incremental assignment operation
__iadd__(self, other) Define the behavior of assignment addition :+=
__isub__(self, other) Define the behavior of subtraction by assignment :-=
__imul__(self, other) Define the behavior of assignment multiplication :*=
__itruediv__(self, other) Define the behavior of assignment division :/=
__ifloordiv__(self, other) Defines the behavior of division of assigned integers ://=
__imod__(self, other) Define the behavior of assignment modulus algorithm :%=
__ipow__(self, other[, modulo]) Define the behavior of the assignment power operation :**=
__ilshift__(self, other) Defines the behavior of bitwise shift of assignment :<<=
__irshift__(self, other) Defines the behavior of bitwise right shifting of assignment :>>=
__iand__(self, other) Define the behavior of assignment bitwise and operation :&=
__ixor__(self, other) Define the behavior of bitwise exclusive or operation of assignment :^=
__ior__(self, other) Defines the behavior of assignment by bit or operation :|=
Unary operators
__pos__(self) Define positive behavior :+x
__neg__(self) Define the behavior of the minus sign :-x
__abs__(self) Definition should be abs() Behavior at call time
__invert__(self) Define the act of bitwise negation :~x
Type conversion
__complex__(self) Definition should be complex() Behavior at call time ( You need to return the appropriate value )
__int__(self) Definition should be int() Behavior at call time ( You need to return the appropriate value )
__float__(self) Definition should be float() Behavior at call time ( You need to return the appropriate value )
__round__(self[, n]) Definition should be round() Behavior at call time ( You need to return the appropriate value )
__index__(self)

1. When an object is applied to a slice expression , Implementation of plastic cast
2. If you define a custom numeric type that may be used in slicing , You should define __index__
3. If __index__ Defined , be __int__ It also needs to be defined , And return the same value

Context management (with sentence )
__enter__(self)

1. Define when using with Initialization behavior at statement time
2. __enter__ The return value of is with The target of the statement or as After the name binding

__exit__(self, exc_type, exc_value, traceback)

1. Define what the context manager should do when a code block is executed or terminated
2. It is usually used to handle exceptions , Clean up the work or do some routine work after the code block execution

Container type
__len__(self) Definition should be len() Behavior at call time ( Returns the number of elements in the container )
__getitem__(self, key) Defines the behavior of getting the specified elements in the container , amount to self[key]
__setitem__(self, key, value) Defines the behavior of the specified element in the settings container , amount to self[key] = value
__delitem__(self, key) Defines the behavior of deleting a specified element in a container , amount to del self[key]
__iter__(self) Define the behavior of elements in the iteration container , encounter StopIteration Abnormal exit
__next__(self) Define the behavior when the iterator takes the next data , Return to the next data
__reversed__(self) Definition should be reversed() Behavior at call time
__contains__(self, item) Define when using member test operators (in or not in) The behavior of time

Python More articles about special properties and methods in

  1. Python in optionParser How to use the module [ turn ]

    In this paper, the form of a more detailed example of Python in optionParser How to use the module , For in-depth learning Python It has a good reference value . Share it with you for your reference . The specific analysis is as follows : Generally speaking ,Python There are two of them ...

  2. python Magic attributes in

    Catalog 1. __doc__ 2. __module__ and __class__ 3. __init__ 4. __del__ 5. __call__ 6. __dict__ 7. __str__ 8. ...

  3. python Bind properties and methods to objects and __slots__ Use

    # With c Language is mainly static language , Compile before running , You are not allowed to edit code while running # In the process of running , You can change , Properties can be added , It's a dynamic language (python) # python Add properties and methods dynamically class ...

  4. python Instance properties and class properties in

    stay python in , What's the difference between class properties and instance properties ? I think it's the difference in scope , Instance objects can access class properties , Class objects cannot access instance properties .( The concept of class itself is the concept of scope , You can't make a cat fly , Cats belong to the cat family , None of these can fly ...

  5. python Private properties and methods of classes ( Reprint )

    Reprint :http://www.runoob.com/python/python-object.html Class properties and methods Private properties of class __private_attrs: Start with two underscores , Declare the property private , You can't ...

  6. Python To prevent sql Detailed explanation of injection method

    SQL Injection is one of the most common network attacks , It's not using the operating system BUG To achieve an attack , It's about the negligence of the programmer in programming , adopt SQL sentence , Realize no account login , Even tampering with the database . The following article mainly introduces about Python in ...

  7. ListBox Binding in Item Template ListBoxItem Method of attribute

    original text :ListBox Binding in Item Template ListBoxItem Method of attribute <ListBox> <ListBox.ItemTemplate> <DataTemplate> & ...

  8. JavaScript Special properties and methods in the event object of ( mouse , keyboard )

    Special properties and methods in the event object caused by mouse operation Mouse events are Web The most common type of events above , After all, the mouse is still the main positioning device . Then the mouse button information and screen coordinates can be obtained through the event object Mouse button Only in the main mouse button sheet ...

  9. python Summary of dictionary built-in methods in

    #!/usr/local/bin/python3 # -*- coding:utf-8 -*- #key-value #dict disorder , No subscript , No subscript required , Because there is key stu={ 'stu001': ...

Random recommendation

  1. easyUI datagrid According to the query conditions Select the row corresponding to the data

    Start Input potatoes , pumpkin , And then there's the green vegetables , Every time you type and click search ( Fuzzy query ), The row corresponding to the selected current data Before doing , I inquired a lot of information on the Internet , I've asked many times in the technology group , It's been a long time. It's finally over . I don't know what to write when I blog for the first time ...

  2. iOS status bar --- Learning Notes 6

    One . Set the color of the status bar . //1. When you need to customize navigation , Set the top view - (UIViewController *)childViewControllerForStatusBarStyle{ retur ...

  3. Java Design patterns - The proxy pattern (Proxy)

    In fact, the name of each pattern indicates the function of the pattern , The proxy pattern is that one more proxy class comes out , Do some operations for the original object , For example, when we rent a house, we go back to find an intermediary , Why? ? Because you don't have comprehensive information about houses in this area , Hope to find a more familiar person to help you ...

  4. ajax Output a two-dimensional array to the front desk And analyze

    Recently, I'm working on a function of after-sales data statistics , It needs to count all kinds of customers in a specific period . The distribution of the models , only table It's rigid to calculate and display ( It's not cool at all ) So I decided to use jquery plug-in unit flot And pass ajax To transmit data :flot The discount of ...

  5. JAVA static effect

    static Express “ overall situation ” perhaps “ static state ” It means , Used to modify member variables and member methods , Can also form static static Code block , however Java There is no concept of global variable in language . By static The modified member variables and member methods are independent of any ...

  6. turn :PHP What developers should know 24 Databases

    Original from :http://blog.jobbole.com/54201/ As a PHP developer , This is an exciting time . A lot of useful libraries are distributed every day , stay Github It's easy to find and use these libraries on . Here is ...

  7. JqueryUI-1

    This article is to consolidate the foundation Learning website :http://jqueryui.com/ Basic concepts :jQuery UI[ In order to jQuery Based on open source JavaScript Web user interface code base . Including the underlying user interaction . Animation . ...

  8. AngularJS + Node.js + MongoDB Development

    AngularJS + Node.js + MongoDB Developed location based address book (by vczero) One . Gossip One day the monitor said , Students hope that I can develop a shared location address book , So I simply designed the next function . package ...

  9. O365 Manager Plus Help desk delegation Introduction

    O365 Manager Plus Help desk delegation Introduction although Office 365 Allows you to work anywhere in the world , But the management function it provides is very insufficient . When an organization is distributed in more than one country / Regional time , It's difficult for an administrator to manage all users and mailbox independently . ...

  10. hashContext

    java.lnag.Object Chinese vs hashCode The agreement of : 1. During the execution of an application , If an object's equals If the information used in the method comparison has not been modified , Call... On the object hashCode Method many times , It will ...