Python keyword triggered magic method

with:

  __enter__

  __exit__

 

= DIC {} 
acquisition activity specified element container 
DIC [ " name " ] = " Egon "  # settings   

the __setitem__ DIC [ " name " ] # values __getitem__

 

Create a class
     __call__ 

create objects 
    __new__ 

    # If you return a string __new__, then it will not take their own __init__, but to go __init__ str the 

constructor 
  __init__ 
  When an instance is created initialization properties Methods
Destructor 

    the __del__ 
     # method is triggered when the memory is released when the garbage collection recovery Example off a space

 

Take property values
         __getattr__ 
        # when using dot obtain an instance property, if the property does not exist automatically call __getattr__ method 

to set property values 
        __setattr__ 
        # When setting instance properties automatically call __setattr__ 
delete attributes
    __delattr__ 

access existing property 
    __getattribute__ (Self, name)
    # object calls through the point of an existing property values

 

Length of the vessel
         the __len__ (Self)
         # Returns the length of the vessel

 

Container iterator
         __iter__ (Self)
         # must be included when making this method iterator, especially when the built-in iter () method is called, and when to use for x in container: When a cyclical fashion. Iterator object themselves, they must be self definition __iter__ method returns.

 

Sequence reversal
         __reversed__ (Self)
         # When reversed () is called when the behavior sequence may be ordered only if the time to implement it, for example, a list or tuple.

 

Call behavior in and not in time to test whether the presence of members of the produced
         __contains__ (Self, Item)
         # If not defined, then Python will iteration container elements to compare one by one, so decided to return True or False

 

Double-stranded sequence could not find a container element triggered by key
         __missing__ (Self, key)
         # dict dictionaries have this method, it defines the behavior If you can not find the key trigger in the container. For example d = { 'a': 1 }, when you execute d [ 'name'], d .__ missing __ [ 'name'] will be called.

 

Reflection
         __instancecheck__ (Self, instance)
         # Check instances are instances of a class not defined your 

        __subclasscheck__ (Self, subclass)
         # subclass of a class is not your check-defined class of

 

Copy
         __copy__ (Self)
         # Behavior When calls to your class's instance copy.copy () generated, shallow copy 

        __deepcopy__ (Self, memodict = {})
         # When calls to your class instance copy.deepcopy () behavior arising, deep copy

 

Compare
         __cmp__ (Self, OTHER)     # is a more basic methods inside magic methods 

        __eq__ (Self, OTHER)     # define the behavior of an equal sign, == 
    
        __ne__ (Self, OTHER)      # Define Unequal symbol behavior! = 

        __Lt__ (Self, OTHER)        # define smaller than the symbol behavior, < 

        __gt__ (Self, OTHER)       #   define the behavior of greater than symbols,> 

        __le__ (Self, OTHER)       # define less than equal to the symbol behavior, <= 
    
        __ge__ (Self, OTHER )      # define the behavior of greater than or equal symbol,> =  

 

Unary operators and functions
         __pos__ (Self)    # define the behavior of a negative number: the -X- 
 
        __neg__ (Self)   # define the behavior of the positive numbers: + X 

        __abs__ (Self)    #   achieve a built-in ABS () function behavior 

        __invert__ ( Self)   #   implement a negation operator (~ operator) acts 

        __round__ (Self, n-) # achieve a built-in round () function behaves 

        __floor__ (Self) # implemented Math.floor () function behavior 

        __ceil__ ( Self)   # achieve math.ceil () function behavior 

        __trunc__ (Self) # achieve math.trunc () function behavior

 

Binocular operators and functions
         __add__ (Self, OTHER)   # implement an addition 
    
        __sub__ (Self, OTHER)    # implement a subtraction 

        __mul__ (Self, OTHER)    # performing a multiplication 

        __floordiv__ (Self, OTHER)   # achieve a "//" operation divisible operator () operator generated 

        __div__ (Self, OTHER)    # achieve a "/" operator representative of the division operation 

        __truediv__ (Self, OTHER)   #   achieve real division 

        __mod__ (Self, OTHER)    # achieve a "%" operator representatives modulo operation 
    
        __divmod__ (Self, OTHER)   # implement a built-in function divmod () 

        __pow__  # achieve an exponential operation ( "*" operator) act or power () call 

        __lshift__(Self, OTHER)   # implement a bit left shift (<<) function 

        __rshift__ (Self, OTHER)   # implement the function of one-bit right shift (>>) of 

        __and__ (Self, OTHER)     # implement a bitwise with operation (&) behavior 

        __or__ (Self, OTHER) # implement a bitwise ORed behavior 

        __xor__ (Self, OTHER)    #    __xor __ (Self, OTHER)   

 

Increment
         __iadd__ (Self, OTHER)   #   addition assignment 

        __isub__ (Self, OTHER)   # subtraction assignment 
    
        __imul__ (Self, OTHER)   # multiplication assignment 

        __ifloordiv__ (Self, OTHER)   # divisible assignment, in addition to the floor, corresponds to the // operator = 

        __idiv__ (Self, OTHER)   # division assignment, corresponding to / = operator 

        __itruediv__ (Self, OTHER)   # Zhenchu assignment 

        __imod_ (Self, OTHER)    # mode assignment operator = equivalent% 

        __ipow__   #   power assignment, corresponding to ** = operator 

        __ilshift__ (Self, OTHER)   #   left assignment operator corresponds << = 

        __irshift__ (Self, OTHER)   # Left assignment = operator >> equivalent 

        __iand__ (Self, OTHER)   # and assignment operator = & equivalent 

        __ior__ (Self, OTHER)    # or assignment 

        __ixor__ (Self, OTHER)   # exclusive OR operator, rather ^ = operator to

 

Type conversion
         __int__ (Self)   # convert integer 
    
        __long__ (Self) # convert integer growth 

        __float__ (Self)   # is converted to floating-point 

        __complex__ (Self)   # converted into a complex type 

        __oct__ (Self)   # convert octal 

        __hex__ (Self )   # converted to hexadecimal 

        __index__ (Self)   # If you define a value type could be used for slicing operation, you should define __index__ 
    
        __trunc__ (Self)    # is when math.trunc (self) use __trunc__ call return own type integer taken 

        __coerce__ (self, OTHER) # performing hybrid type operation 
    
     __bytes__ (self)  # Is bytes behavior when the call () 

     __hash__ (Self)  # behavior is hash () call 
    
     __bool__ (Self)  # () behavior when the call is BOOL 
  
     __format__ (Self, format_spec)    # is () behavior when calling format 
    
     __repr__ (Self)   # behavior is () call repr 

     __DIR__ (Self)   # when dir () is called behavior

 

Guess you like

Origin www.cnblogs.com/gwklan/p/11140127.html