Python learning notes 1-metaclass _ metaclass __, metaclass _
Type is actually a meta-class, and type is the rule for creating classes in the meta-class python for all objects created behind python: Suppose you create the Foo class.
class Foo(Bar): def __init__(): pass
Purpose: The primary purpose of a metadatabase is to automatically change a class when a class is created. The primary purpose of a metadatabase is to create an API. A typical example is Django ORM. It allows you to define the Flask sqlalchemy ORM class as follows: the definition of the Flask sqlchemy ORM class also inherits a created class Base. Note that all table classes in sqlalchemy must inherit a Base object, otherwise, the created Base table cannot implement ORM ing; sqlalchemy instance:
From sqlalchemy. ext. declarative import declarative_baseBase = declarative_base () class HotWordType (Base): # table name _ tablename _ = 'hotwordtype' # id typeName id = Column (Integer, primary_key = True) # primary key typeName = Column (String (20), nullable = False) # type name hotWord = relationship ('hotword', backref = 'hotwordtype ')
Source code of the metabase in declarative_base:
The new base class will be given a metaclass that producesappropriate :class:`~sqlalchemy.schema.Table` objects and makesthe appropriate :func:`~sqlalchemy.orm.mapper` calls based on theinformation provided declaratively in the class and any subclassesof the class.class DeclarativeMeta(type): def __init__(cls, classname, bases, dict_): if '_decl_class_registry' not in cls.__dict__: _as_declarative(cls, classname, cls.__dict__) type.__init__(cls, classname, bases, dict_) def __setattr__(cls, key, value): _add_attribute(cls, key, value)