Python learning notes 1-metaclass _ metaclass __, metaclass _

Source: Internet
Author: User

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) 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.