What is day 29 yuan class, class analysis underlying principle, to control the generated class by metaclass metaclass control process by calling the class, with the properties look after metaclass

What is the metaclass

# Everything Object: a class is actually an object 
#Person class is also a target, he must be an example of a class derived from this class, called the metaclass
#type is a built-dollar class, all classes are examples of the type obtained by the
# class generated class, called the metaclass
class the Person:
DEF the __init __ (Self, name):
the self.name = name
DEF score (Self):
Print ( 'score is 100')

P = the Person ( 'Nick ')

# A = the Person
# p1 = A (' Nick ')
# Print (p1.name)

how to find metaclass?
Print # (of the type (the p-))
# the same way: type class is to produce all kind of metaclass
# Print (of the type (the Person))
Print (of the type (dict))
Print (of the type (List))
Print (of the type (str))
Print (type (Object))
Print (type (type))


class underlying principles
#class constructed class name of the class will, in fact: metaclass instantiate the object class is generated, generates an object class is instantiated, it must be: class name () #Person class is instantiated by the type produced, a bunch of parameters passed
#class underlying call type is generated to instantiate class (object)
__init__ method #type () call the class? ? ? 
#type (object_or_name, bases, dict)
object_or_name: class name, a string of
bases: all of its parent class, the base class is a tuple
dict: namespace, a dictionary is

L = {}
Exec ( '' '
School =' Oldboy '
DEF the __init __ (Self, name):
the self.name = name
DEF score (Self):
Print (' score is 100 ')
' '', {}, L)
Person=type('Person',(object,),l)
或者
def __init__(self,name):
self.name=name
Person=type('Person',(object,),{'school':'oldboy','__init__':__init__})



#print(Person.__dict__)
#print(Person.__bases__)
# p=Person('nick')
# print(p.name)
# print(p.__dict__)



# class Person:
# school='oldboy'
# def __init__(self,name):
# self.name=name
# def score(self):
# print('分数是100')
# a=Person
#
# p=Person('nick')

exec()的用法
# l={}
# exec('''
# school='oldboy'
# def __init__(self,name):
# self.name=name
# def score(self):
# Print ( 'score is 100')
# ''',{},l)

# print(l)
# g={ 'x':1,'y':2}
# l={}
# exec('''
# global x
# x=100
# z=200
# m=300
# ''',g,l)
# print(g)
# print(l)

# x=1
# y=2
# def test():
# global x
# x = 100
# z = 200
# m = 300


Produced is controlled by metaclass class
custom metaclass to control the production of class, you can control the class name, you can control inherit the parent class, control class namespace

custom metadata class must inherit type, write a class that inherits type this the type of call metaclass

class Mymeta (of the type):
DEF __init __ (Self, name, bases, dic):
# Self is the Person category
Print (name)
Print (bases)
Print (dic)
# exercise 1: unrestricted control class name must beginning with SB
IF not name.startswith ( 'SB'):
the raise Exception ( 'class name does not begin with SB')




class the Person (Object, the metaclass that = Mymeta): # = Mymeta the metaclass that specify when the class generation, with their own write this meta-class of Mymeta
     '' ' 
Note
' ''
School = 'Oldboy'
DEF the __init __ (Self, name):
the self.name = name
DEF Score (Self):
Print ( 'score is 100')

P = the Person ()
# Exercise Two: class must add a comment
class Mymeta (type): 
DEF the __init __ (Self, name, bases, DIC):
Print (Self .__ dict __ [ '__ doc__ displays a'])
DOC = Self .__ dict __ [ '__ doc__ displays a']
IF Not DOC:
# No footnote
raise Exception ( 'your class does not add comments')
class the Person (Object, the metaclass that = Mymeta):
'' '
I added a note
' ''
School = 'Oldboy'
DEF __init __ (Self, name):
self.name name =
DEF Score ( Self):
Print ( 'score is 100')




By controlling metaclass class called procedure
# __call__ be used to
call control process # class, in essence, the control of: generating object
class Mymeta (type):
DEF the __call __ (Self, args *, ** kwargs):
Print ( 'XXX')

return. 1

class the Person (Object, the metaclass that = Mymeta):
School = 'Oldboy'
DEF the __init __ (Self, name):
the self.name = name
DEF score (Self):
Print ( 'score is 100')

P = the Person ( 'Nick')
Print (p.name)
# printing xxx, and p no name attribute


class the Person ():
School = 'Oldboy'
DEF the __init __ (Self, name):
the self.name = name
DEF Score (Self):
print ( 'score is 100')
DEF the __call __ (Self, args *, ** kwargs):
print('xxxx')

p = Person ( 'nick') # init automatically trigger the execution
p () # first trigger __call__ yuan class, that __call__ instantiated object or instance of the class will be triggered when the brackets call __call__
#__new__ method
# Exercise: all attributes object are set to private
analysis
class Mymeta (of the type):
DEF __call __ (Self, * args, ** kwargs):
#self this class is Person
# Print (args)
# Print (kwargs)
# Self return (* args) # not here, recursively

# instantiate an object to produce a Person class, with __new__ to produce, you need to pass class in the past, in order to produce the object
#obj is the object of the Person class, just empty
obj = new new Object .__ __ (Self)
# or obj = self .__ new __ (self )
         # Call the __init__ method to complete the initialization 
# class to call the __init__ method, it is a normal function, there are several parameters to wear a few miserable
Self .__ the init __ (obj, * args, ** kwargs)
# object to call __init_ _ method, binding method object will be passing through the self
obj .__ the init __ (* args, ** kwargs)
Print (obj)
return obj


class the Person (object, the metaclass that = Mymeta):
School = 'Oldboy'
DEF the __init __ (self , name):
the self.name = name
DEF score (Self):
Print ( 'score is 100')
P = the Person (name = 'Nick')
Print (P)
Print (p.name)

# all the properties of the object are become private
class Mymeta (of the type):
DEF __call __ (Self, * args, ** kwargs):
obj = Object .__ __ new new (Self)
obj .__ the init __ (* args, ** kwargs)
# print(obj.__dict__)
obj.__dict__={ '_%s__%s'%(self.__name__,k):v for k,v in obj.__dict__.items()}
# print(obj.__dict__)
return obj

class Person(object, metaclass=Mymeta):
school = 'oldboy'
def __init__(self, name):
self.name = name
def score(self):
print('分数是100')
p = Person(name='nick')
print(p.__dict__)
print(p.name)
# print(p)
# print(p.name)







 

Guess you like

Origin www.cnblogs.com/wwei4332/p/11454150.html