python and a reflector Metaclass

One, Moto类

Yuan class is class, which is used to create the kind of class

By default all classes are type yuan

As long as the inherited type then this class becomes a meta-class.

Custom metaclass:

class MyType (of the type):
     DEF  __init__ (Self, clss_name, bases, dict): 
        Super (). __init__ (clss_name, bases, dict)
         Print (clss_name, bases, dict)
         IF  not clss_name.istitle ():
             The raise Exception ( " Ah you can not write the name of the class ... " ) 

# specifies the metaclass for the class MyType as a pig 
class Pig (the metaclass that = MyType):
     Pass 

class Duck (the metaclass that = MyType):
     Pass

Second, metaclasses call method:

When you call a class object will automatically perform __call__ metaclass, and as the first argument to the class itself.
After covering metaclasses call, this class will not be able to produce the object, the object must be created to accomplish with super () .__ call__, but also the return value

scenes to be used:

When you want to control the process of creating an object, it will cover the call method.

When you want to create a process control class will cover the init method.

To achieve the objects of all the attribute names to uppercase: 4

lass MyType(type):
    def __call__(self, *args, **kwargs):
        new_args = []
        for a in args:
            new_args.append(a.upper())

        print(new_args)
        print(kwargs)
        return super().__call__(*new_args,**kwargs)


class Person(metaclass=MyType):
    def __init__(self,name,gender):
        self.name = name
        self.gender = gender

p = Person(name="jack",gender="woman")
print(p.name)
print(p.gender)

2.new method

When you create a class object, the method will be executed first __new__ yuan class, and get an empty object, and then will automatically call the __init__

Initialization method.

If you override this method, and that new methods must have a return value, and the return value must be a class object

class Meta (of the type): 

    DEF  __new__ (CLS, * args, ** kwargs):
         Print (CLS) # metaclass own 
        Print (args) # create several parameters required for the class name of the class, the base class, namespaces 
        Print (kwargs ) # empty 
        Print ( " new new RUN " )
         # return Super () .__ new new __ (CLS, * args, ** kwargs) 
        obj = of the type. __new__ (CLS, * args, ** kwargs)
         return obj
     DEF  __init__ (Self, A, B, C): 
        . Super () the __init__ (A, B, C)
         Print("init run")
class A(metaclass=Meta):
    pass
print(A)

Example 3. Single:

Refers to a class object is to produce a

Singleton in order to save resources, when all the object properties of a class all the same, there is no need to create a plurality of objects

Yuan class implementation:

Example # n-type single
class Single (type):
DEF the __call __ (Self, args *, ** kwargs):
IF the hasattr (Self, "obj"): # determines whether or not there has been some target
return getattr (self, "obj ") has returned #

obj = super () .__ call __ (* args, ** kwargs) # do not create
print ( "new up")
self.obj obj = # and stored in class
return obj


class Student(metaclass=Single):
def __init__(self,name):
self.name = name


class Person(metaclass=Single):
pass

# Will create an object
the Person ()
the Person ()

 

 

 

Third, reflection

Reflecting means should have an object can be detected, modify, increase their ability to attribute

In fact reflected by the string operation property

1.hasattr: to determine whether a property of an object exists

2.getattr: Remove a property from an object

3.setattr: add a property to an object

4.delattr: delete a property from an object

 

Guess you like

Origin www.cnblogs.com/wujc3/p/11272430.html