A method of object-oriented Python base class and the class-related attributes, and the reflective magic

A : class methods
common method: no parameters, only class calls the
bound method: (1) bind to the object (object parameter passed automatically) (2) bind to class (automatic transmission parameter)
static method: either method in class or object can call

#example:
class Dog ():
    DEF __init __ (Self, name):
        self.name = name

    # ordinary method
    DEF jiao ():
        Print ( "puppy will call")

    # bind to object methods
    DEF tian (Self):
        Print ( "like licking dog bones")

    # bound to the class method
    @classmethod
    DEF Chi (CLS):
        Print (CLS)
        Print ( "dog like to eat meat")

    # static method
    @staticmethod
    DEF Jump ():
        Print ( "dog then jump like Frisbee")

#instantiate the object
obj = Dog ( "black")

# (1) an ordinary method
Dog.jiao ()
# object can not be invoked only by the class. Method

# (2) bound to the object method
obj.tian ()

# Dog.tian (123) using # If insist class to call, meet an argument corresponding to a (stochastic values)

# (3) bound to the object method
Dog.chi ()
# object-bound calls to the method are possible class
# to find out who belongs to the object class is then this class transmitted
obj.chi ()

#  (. 4) static method
obj.jump ()
Dog.jump ()

Two : Magic attributes associated with the class
#Example:

class Human():
    pass

class Man():
    pass

class Woman():
    pass


Children class (Man, Woman):
    '' '
    functions like: describes a child
    member of the class attribute : eye
    member methods class: SLEEP __beat_doudou
    ' ''
    Eye = "blue eyes"
    # common method
    def eat ():
        print ( "children need to eat something nutritious")

    # bind method
    DEF sLEEP (Self):
        print ( "kids like to sleep")

    DEF. drink. (Self):
        print ( "children should always drink plenty of water")

    DEF Cry ( Self, FUNC):
        RES = FUNC .__ name__
        Print (RES, of the type (RES))

    # private-bound method
    DEF __beat_doudou (Self):
        Print ( "kids like to play Peas")

#__dict__ get the internal structure of the object or class of members
Children = obj ()
RES = obj.__dict__
# __dict__ ownership is the class of all objects with no way to view the internal structure of a member of the class
print (res) # so res empty
Print (Children .__ dict__ magic)

# the __doc__ acquired within the document object or class
res = Children .__ doc__ displays a
Print (res)

'' '
output:
    Class Functions: describes a child
    member of the class attribute : eye
    member methods of the class: SLEEP __beat_doudou
'' '
# __name__ get the class name of the function name
DEF myfunc ():
    Print ( "I'm function")
obj.cry (myfunc) # myfunc <class' str'>
# __class__ get current class the object belongs
RES = obj .__ class__ is
Print (RES) # <class 'in __main __. Children'>

# __bases__ obtaining a class inherits directly all the parent class , returns a tuple
RES = Children .__ bases__
Print (RES)

Three : reflection
# Concepts : by the string class object to manipulate or module attribute Method
# (1) hasattr () detects an object / class if there is a specified member  [ to automatically invoke the basis of whether ]
#use the II theobj

RES = the hasattr (obj, "Eye")
Print (RES)

RES = the hasattr (Children, "EAT")
Print (RES) # returns True specify the presence

# (2) getattr () Gets an object / class membership value
res = getattr (obj, "eye")
Print (RES) # blue eyes

RES = getattr (obj, "sLEEP")
Print (RES) # it returns a bound method
res () # child likes to sleep

mode # class
= getattr RES (children, "eAT")
Print (RES)
RES () # child needs to eat something nutritious

RES = getattr (children, "SLEEP")
Print (RES) # it is not a binding method, all must You need a way like
RES (1)

# getattr third parameter is optional if the acquisition is less than this value, you can add default prompts, error prevention
res = getattr (obj, "abc ", " I'm sorry that I did not")
Print ( RES)

# = strvar the iNPUT ( "Please enter the function you want to call:")
# if hasattr(obj,strvar):
Getattr RES = # (obj, strvar)
# RES ()

# (. 3) setattr () Sets the object / value class members
setattr (obj, "hair", " black")
Print (obj.hair)
setattr (Children, " skin "," green ")
Print (Children.skin)

# (4) delattr () delete an object / value class members
delattr (obj," Hair ")
# Print (obj.hair)

delattr (Children," Skin ")
Print # (Children.skin)

# (2) reflecting the module
# sys.modules returns a system dictionary , the dictionary keys are all loaded modules

# Example . 3:
Import SYS
RES = the sys.modules
Print (RES)
Print (__ name__) # __main__ master file whether running
MyModule the sys.modules = [__ name__]
Print (MyModule) # printing master module

DEF func1 ():
    Print ( " this method is func1 ")

DEF func2 ():
    Print (" it's func2 method ")

DEF func3 ():
    Print (" it's func3 method ")

# users to string me, I reflected corresponding method calls
while True:
    strvar = input ( "Please enter the method you want to call:")
    IF hasattr (mymodule, strvar):
        _func_ = getattr (mymodule, strvar)
        _func_ ()
        # Note that this function will inherit the functions you entered,
        # all if the input _ myfunc will print the contents of a function, the function names all get a little special
    the else:
        Print ( "Big brother, no.")

 

Guess you like

Origin www.cnblogs.com/hszstudypy/p/10964067.html