Skip to main content
added 450 characters in body; deleted 101 characters in body
Source Link
Xingzhou Liu
  • 1.6k
  • 9
  • 13
class somemeta(type): __new__(mcs, name, bases, clsdict): """ mcs: is the base metaclass, in this case type. name: name of the new class, as provided by the user. bases: tuple of base classes clsdict: a dictionary containing all methods and attributes defined on class you must return a class object by invoking the __new__ constructor on the base metaclass. ie: return type.__call__(mcs, name, bases, clsdict). in the following case: class foo(baseclass): __metaclass__ = somemeta an_attr = 12 def bar(self): ... @classmethod def foo(cls): ... arguments would be : ( somemeta, "somemeta""foo", (baseclass, baseofbase,..., object), {"an_attr":12, "bar": <function>, "foo": <bound class method>} you can modify any of these values before passing on to type """ return type.__call__(mcs, name, bases, clsdict) def __init__(clsself, name, bases, clsdict): """ called after type has been created. unlike in standard classes, __init__ method cannot modify the instance (cls) - and should be used for class validaton. """ pass def __prepare__(): """ returns a dict or something that can be used as a namespace. the type will then attach methods and attributes from class definition to it.   call order :  somemeta.__new__ -> type.__new__ -> type.__init__ -> somemeta.__init__ """ return dict()    def mymethod(cls): """ works like a classmethod, but for class objects. Also, my method will not be visible to instances of cls. """ pass 

Call order in above example:

class somemeta(type): __new__(mcs, name, bases, clsdict): """ mcs: is the base metaclass, in this case type. name: name of the new class, as provided by the user. bases: tuple of base classes clsdict: a dictionary containing all methods and attributes defined on class you must return a class object by invoking the __new__ constructor on the base metaclass. ie: return type.__call__(mcs, name, bases, clsdict). in the following case: class foo(baseclass): __metaclass__ = somemeta an_attr = 12 def bar(self): ... @classmethod def foo(cls): ... arguments would be : ( somemeta, "somemeta", (baseclass, baseofbase,..., object), {"an_attr":12, "bar": <function>, "foo": <bound class method>} you can modify any of these values before passing on to type """ return type.__call__(mcs, name, bases, clsdict) def __init__(cls, name, bases, clsdict): """ called after type has been created. unlike in standard classes, __init__ method cannot modify the instance (cls) - and should be used for class validaton. """ pass def __prepare__(): """ returns a dict or something that can be used as a namespace. the type will then attach methods and attributes from class definition to it. somemeta.__new__ -> type.__new__ -> type.__init__ -> somemeta.__init__ """ return dict() 

Call order in above example:

class somemeta(type): __new__(mcs, name, bases, clsdict): """ mcs: is the base metaclass, in this case type. name: name of the new class, as provided by the user. bases: tuple of base classes clsdict: a dictionary containing all methods and attributes defined on class you must return a class object by invoking the __new__ constructor on the base metaclass. ie: return type.__call__(mcs, name, bases, clsdict). in the following case: class foo(baseclass): __metaclass__ = somemeta an_attr = 12 def bar(self): ... @classmethod def foo(cls): ... arguments would be : ( somemeta, "foo", (baseclass, baseofbase,..., object), {"an_attr":12, "bar": <function>, "foo": <bound class method>} you can modify any of these values before passing on to type """ return type.__call__(mcs, name, bases, clsdict) def __init__(self, name, bases, clsdict): """ called after type has been created. unlike in standard classes, __init__ method cannot modify the instance (cls) - and should be used for class validaton. """ pass def __prepare__(): """ returns a dict or something that can be used as a namespace. the type will then attach methods and attributes from class definition to it.   call order :  somemeta.__new__ -> type.__new__ -> type.__init__ -> somemeta.__init__ """ return dict()    def mymethod(cls): """ works like a classmethod, but for class objects. Also, my method will not be visible to instances of cls. """ pass 
added 450 characters in body; deleted 101 characters in body
Source Link
Xingzhou Liu
  • 1.6k
  • 9
  • 13

meta class are used to apply some rule to an entire set of classes. For example, suppose you're building an ORM to access a database, and you want records from each table to be its ownof a class mapped to that table (based on fields, business rules, etc..,), a possible use of metaclass is for instance, connection pool logic, which is share by all table record classes of record from all tables. Another use is logic to to support foreign keys, which involves multiple classes of records.

Another use is if you want to customize namespacing, as some have already explained.

meta class are used to apply some rule to an entire set of classes. For example, suppose you're building an ORM to access a database, and you want each table to be its own class (based on fields, business rules, etc..,), a possible use of metaclass is for instance connection pool logic, which is share by all table record classes. Another use is logic to to support foreign keys, which involves multiple classes of records.

Another use is if you want to customize namespacing, as some have already explained.

meta class are used to apply some rule to an entire set of classes. For example, suppose you're building an ORM to access a database, and you want records from each table to be of a class mapped to that table (based on fields, business rules, etc..,), a possible use of metaclass is for instance, connection pool logic, which is share by all classes of record from all tables. Another use is logic to to support foreign keys, which involves multiple classes of records.

added 450 characters in body; deleted 101 characters in body
Source Link
Xingzhou Liu
  • 1.6k
  • 9
  • 13

meta class are used to apply some rule to an entire set of classes. For example, suppose you're building an ORM to access a database, and you want each table to be its own class (based on fields, business rules, etc..,), a possible use of metaclass is for instance connection pool logic, which is share by all table record classes. Another use is logic to to support foreign keys, which involves multiple classes of records.

class somemeta(type): __new__(mcs, name, bases, clsdict): """ mcs: is the base metaclass, in this case type. name: name of the new class, as provided by the user. bases: tuple of base classes clsdict: a dictionary containing all methods and attributes defined on class you must return a class object by classinvoking the new__new__ constructor on the base metaclass. ie: return type.__call__(mcs, name, bases, clsdict). in the following case: class foo(baseclass): __metaclass__ = somemeta an_attr = 12 def bar(self): ... @classmethod def foo(cls): ... arguments would be : ( somemeta, "somemeta", (baseclass, baseofbase,..., object), {"an_attr":12, "bar": <function>, "foo": <bound class method>} you can modify any of these values before passing on to type """ return type.__call__(mcs, name, bases, clsdict)   def __init__(cls, name, bases, clsdict): """ called after type has been created. unlike in standard classes, __init__ method cannot modify the instance (cls) - and should be used for class validaton. """ pass   def __prepare__(): """ returns a dict or something that can be used as a namespace. the type will then attach methods and attributes from class definition to it. somemeta.__new__ -> type.__new__ -> type.__init__ -> somemeta.__init__ """ return dict() 

Call order in above example: somemeta.new -> type.new -> type.init -> somemeta.init

anyhow, those two are the most commonly used hooks. metaclassing is powerful, but should use judiciously.and above is nowhere near and exhaustive list of uses for instance, validating classes can also be done with abstract classesmetaclassing.

meta class are used to apply some rule to an entire set of classes. For example, suppose you're building an ORM to access a database, and you want each table to be its own class (based on fields, business rules, etc..,), a possible use of metaclass is for instance connection pool logic, which is share by all table record classes.

class somemeta(type): __new__(mcs, name, bases, clsdict): """ mcs: is the base metaclass, in this case type. name: name of the new class, as provided by the user. bases: tuple of base classes clsdict: a dictionary containing all methods and attributes defined on class you must return a class object by class the new constructor on the base metaclass. ie: return type.__call__(mcs, name, bases, clsdict). in the following case: class foo(baseclass): __metaclass__ = somemeta an_attr = 12 def bar(self): ... @classmethod def foo(cls): ... arguments would be : ( somemeta, "somemeta", (baseclass, baseofbase,..., object), {"an_attr":12, "bar": <function>, "foo": <bound class method>} you can modify any of these values before passing on to type """ return type.__call__(mcs, name, bases, clsdict) __init__(cls, name, bases, clsdict): """ called after type has been created. unlike in standard classes, __init__ method cannot modify the instance (cls) - and should be used for class validaton. """ pass 

Call order in above example: somemeta.new -> type.new -> type.init -> somemeta.init

anyhow, those two are the most commonly used hooks. metaclassing is powerful, but should use judiciously. for instance, validating classes can also be done with abstract classes.

meta class are used to apply some rule to an entire set of classes. For example, suppose you're building an ORM to access a database, and you want each table to be its own class (based on fields, business rules, etc..,), a possible use of metaclass is for instance connection pool logic, which is share by all table record classes. Another use is logic to to support foreign keys, which involves multiple classes of records.

class somemeta(type): __new__(mcs, name, bases, clsdict): """ mcs: is the base metaclass, in this case type. name: name of the new class, as provided by the user. bases: tuple of base classes clsdict: a dictionary containing all methods and attributes defined on class you must return a class object by invoking the __new__ constructor on the base metaclass. ie: return type.__call__(mcs, name, bases, clsdict). in the following case: class foo(baseclass): __metaclass__ = somemeta an_attr = 12 def bar(self): ... @classmethod def foo(cls): ... arguments would be : ( somemeta, "somemeta", (baseclass, baseofbase,..., object), {"an_attr":12, "bar": <function>, "foo": <bound class method>} you can modify any of these values before passing on to type """ return type.__call__(mcs, name, bases, clsdict)   def __init__(cls, name, bases, clsdict): """ called after type has been created. unlike in standard classes, __init__ method cannot modify the instance (cls) - and should be used for class validaton. """ pass   def __prepare__(): """ returns a dict or something that can be used as a namespace. the type will then attach methods and attributes from class definition to it. somemeta.__new__ -> type.__new__ -> type.__init__ -> somemeta.__init__ """ return dict() 

Call order in above example:

anyhow, those two are the most commonly used hooks. metaclassing is powerful, and above is nowhere near and exhaustive list of uses for metaclassing.

Source Link
Xingzhou Liu
  • 1.6k
  • 9
  • 13
Loading