python class object class instance creation and

And the class object class instance creation

# Class object class instance creation and 
class AnimalMeta (of the type):
     DEF  __new__ (CLS, * args, ** kwargs):
         Print ( " =======> AnimalMeta__new __% S " % CLS)
         return of the type. __New__ (CLS, args *, ** kwargs) 

    DEF  the __init__ (Self, args *, ** kwargs):
         Print ( " =======> AnimalMeta__init __% S " % Self) 

    DEF  the __call__ (Self, args *, * * kwargs):
         Print ( " =======> AnimalMeta__call __% S " % self)
        return type.__call__(self,*args,**kwargs)


class Animal(metaclass=AnimalMeta):
    def __new__(cls, *args, **kwargs):
        print("=======>Animal__new__%s" % cls)
        return object.__new__(cls, *args, **kwargs)

    def __init__(self):
        print("=======>Animal__init__%s" % self)

    def __call__(self, *args, **kwargs):
         Print ( " =======> Animal__call __% S " % Self) 


class PersonMeat (type): 

    DEF  __new__ is (CLS, args *, ** kwargs):
         # for creating a class object implement according to the return value of different classes of objects to create different, that the class object is determined by the return value of 
        # but there must be a subclass of the object type 
        Print ( " =======> PersonMeat__new __% S " % CLS)
         return of the type. __new__ (CLS, * args, ** kwargs)
         # return of the type .__ new new __ (AnimalMeta, * args, ** kwargs) # class object is created by a subclass of the original class of other 
        # return AnimalMeta (* args, ** kwargs) 

    DEF the __init__ (Self, args *, ** kwargs):
         # for implementing a method for assigning attributes to the class object and 
        Print ( " =======> PersonMeat__init __% S " % Self) 

    DEF  the __call__ (Self, * args , ** kwargs):
         # realize that creates class instances, self is the class object, call first class object __new__ method, based on the return value __new__ object 
        # implementation calls for different instances of a class __init__ method to complete class instance attribute assignment 
        Print ( " =======> PersonMeat__call __% S " % Self)
         return type. the __call__ (Self, args *, ** kwargs)
         # return type .__ Call __ (Animal, * args, kwargs **) 
        #Animal return (* args, ** kwargs) 

class persion (the metaclass that = PersonMeat): 

    DEF  __new__ (CLS, * args, ** kwargs):
         # realize that creates class instances 
        Print ( " =======> Persion__new __% S " % CLS)
         return . Object __new__ is (CLS, args *, ** kwargs)
         # return new new Object .__ __ (Animal, args *, ** kwargs) 
        # return Animal () 
    DEF  the __init__ (Self):
         # class instance of attribute assignment 
        Print ( " =======> Persion__init __% S " % Self)

    DEF  the __call__ (Self, args *, ** kwargs):
         Print ( " =======> Persion__call __% S " % Self) 

'' ' 
creating instances of process: 
1, first __new_ original class by class _ way to create a class object 
2, in the __init__ method of the original class by class to implement the assignment to the class object properties and methods 
, thus realizing _ the class object of the class by calling the original method __call__ class _new__ __init__ method of scheduling method and 
4, __new__ method calls class object instance of a class of 
5, __init__ method calls implemented valued property class object instance of a class and a method of 
'' ' 
# P = persion ()
View Code

 

Guess you like

Origin www.cnblogs.com/aadmina/p/11069091.html