python3 (twenty-five) getClassInfo

"" "   " "" 
__Author__ = ' shaozhiqi ' 

#   How do we know that the object what type, use type () 
Print (type (123))   # <class 'int'> 
Print (type ( ' ABC ' ))   # <class 'STR'> 
Print (type (None))   # <class 'NoneType'> 
Print (type (ABS))   # <class 'builtin_function_or_method'>
print(type(123) == type(456))  # True
print(type(123) == int)  # True
print(type('abc') == STR)   # True 
Print (type ( ' ABC ' ) == type (123))   # False 
# determines whether an object is a function of 
Import types 


DEF Fn ():
     Pass 


Print (type (Fn) == types.FunctionType )   # True 
Print (of the type (ABS) == types.BuiltinFunctionType)   # True 

# on isinstance () is to determine whether an object is the type itself, or in the type of parent inheritance chain 
# as described in section twenty-four 
# Print (the isinstance (Dog, Animal)) True # 
# can type () can also be substantially determined by the type of the isinstance () 
Print (the isinstance ( ' a ', STR))   # True 
Print (the isinstance (123, int))   # True 
Print (the isinstance (B ' A ' , bytes))   # True 
# the isinstance may determine whether a variable is a certain type of 
Print (the isinstance ( [. 1, 2,. 3 ], (List, tuple))) 

# the dir () ------------------------------- ------------------------- 
# all the properties and methods of obtaining an object, you can use dir () function, which returns a string containing the list 
Print (the dir ( ' the ABC ' ))
 # ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
# Similar __xxx__ properties and methods in Python are all special purpose, such as the method returns __len__ length. 
# In Python, if you call len () function tries to get the length of an object, in fact, inside the len () function, which automatically went to call the object's __len __ () method 
Print (len ( ' ABC ' ))   # 3 
Print ( ' ABC ' . __len__ ())   # 3 


# can also be defined in our own writing class __len__ 
class myDog (Object): 

    DEF  __init__ (Self): 
        self.nameDog = ' Dog ' 

    DEF  __len__ ( Self):
         return 100 DEF RUN (Self):
        

    Print ( ' running ....... ' ) 


Dog = myDog ()
 Print (len (Dog))   # 100 deleted __len__ method given 
Print ( ' the ABC ' .lower ())   # ABC 

Print (the hasattr (Dog , ' nameDog ' ))   # True there you have nameDog property 
Print (hasattr (Dog, ' color ' ))   # False no color attribute 

setattr (Dog, ' color ' , ' black and white ' )   #Setting a property 'Color' 
Print (the hasattr (Dog, ' Color ' ))   # True 
Print (getattr (Dog, ' Color ' ))   # monochrome attribute value acquired Among 
Print (dog.color)   # Among 
Print (getattr (Dog , ' Age ' , ' 404 ' ))   # 404 acquires nonexistent property will be reported error, set the default value processing 404 

# acquired object methods 
Print (the hasattr (Dog, ' RUN ' ))   # True 
Fn = getattr (Dog , ' RUN' ) 
Fn ()   # running ....... 


# ---------- attribute instance ---------------------- -------------------------- 
class Student (Object): 
    name = ' Student ' 

    DEF  the __init__ (Self, name): 
        the self.name = name 


S = Student ( ' Bob ' ) 
s.score = 90
 Print (s.name, ' , ' , s.score)   # Bob, 90 
Print (Student.name)   # Student 
# deleted property 
dels.name
 Print (s.name, ' , ' , s.score)   # Student, 90 
Student.name = ' student1 ' 
Print (s.name, ' , ' , s.score)   # student1, 90 
# If instance attribute and class attributes with the same name, the class instance attributes will overwrite property, so the actual development avoided. Unless there is real business need to do so

 

Guess you like

Origin www.cnblogs.com/shaozhiqi/p/11550453.html