Method special class Python

# Special method, also known as magic methods 
# special methods use __ beginning and end of 
# special way we generally do not need to manually call to be automatically executed in some special cases 

# define a Person class 
class Person (Object):
     "" " human " "" 
    DEF  __init__ (Self, name, Age): 
        self.name = name 
        self.age = Age 

    # __str __ () this particular method will attempt to convert the object to a string when calling 
    # its role It can be used to convert the result of the specified object (Print function) string   
    DEF  __str__ (Self):
         return  ' the Person [name =% S,% = Age D] ' % (the self.name, self.age)        

    #__repr __ () This particular method will be invoked when the current object using the repr () function 
    # Its role is to effect a specified object 'interactive mode' directly output from the     
    DEF  __repr__ (Self):
         return  ' the Hello '  
 
    '' ' object __dir__ method lists all the properties within the object (including the methods) name, the process will 
    returns comprising (method) the sequences of all of the attribute name. When the program executes dir (object) function of an object, 
    in fact, it is the object __dir __ () method returns the values are sorted, and then packaged into a list. '' ' 
    # __Dir __ ():          
    

    ' '' the __dict__ dictionary attribute for all view objects stored therein attribute names and values of the composition, typically a program directly 
    the attribute. See __dict__ property uses both internal state of all objects, can be accessed or modified by dictionary syntax 
    value of the specified property. '' ' 
    # __Dict __ (): 

    # Object .__ the Add __ (Self, OTHER) 
    # Object .__ Sub __ (Self, OTHER) 
    # object.__mul__(self, other)
    # object.__matmul__(self, other)
    # object.__truediv__(self, other)
    # object.__floordiv__(self, other)
    # object.__mod__(self, other)
    # object.__divmod__(self, other)
    # object.__pow__(self, other[, modulo])
    # object.__lshift__(self, other)
    # object.__rshift__(self, other)
    # object.__and__(self, other)
    # object.__xor__(self, other)
    # object.__or__(self, other)

    # object.__lt__(self, other) 小于 <
    # object.__le__(self, other) 小于等于 <=
    # object.__eq__(self, other) 等于 ==
    # Object .__ NE __ (Self, OTHER) is not equal! = 
    # Object .__ gt __ (Self, OTHER) greater than> 
    # Object .__ GE __ (Self, OTHER) greater than or equal> = 
    
    # __len __ () Gets the length of objects 

    # Object .__ BOOL __ ( Self) 
    # can be specified by bool object into a situation Boolean value 
    DEF  __bool__ (Self):
         return self.age> 17 # __gt__ will do relatively greater than the time to call in an object, the return value of this method will be used as comparison results # he takes two arguments, a self represented by this object, other objects represent and compare the current object # self> OTHER DEF __gt__ (self, other):
         return self.age> other.age # create two instances of the Person class         
the Person = P1 ( ' Monkey

    
    
    
     


' , 16 ) 
P2 = the Person ( ' pig ' , 28 ) 

# print P1 
# when we print an object is actually printed is a special object method __str __ () return value 
# Print (P1) # <__ main__ Object AT 0x04E95090 .Person> 
Print (P1)
 Print (P2) 

Print (the repr (P1)) 

# T = l, 2,3 
# Print (T) # (. 1, 2,. 3) 

Print (P1> P2)
 Print ( P2> p1) 

Print (BOOL (p1)) 

# IF p1: 
#      Print (p1.name, 'have grown up') 
# the else: 
#     print (p1.name, 'but also a minor') 

Print (P1. __DIR__ ())
 # Print (the dir (P1)) 
# Print (P1 .__ dict__ magic) 
# P1 .__ dict __ [ "name"] = "WZY" 
# print (p1 .__ dict __ [ " name"])

 

Guess you like

Origin www.cnblogs.com/jzxs/p/11413963.html