以下代码段来自SQLAlchemy source code:
class EngineStrategy(object):
"""An adaptor that processes input arguments and produces an Engine.
Provides a ``create`` method that receives input arguments and
produces an instance of base.Engine or a subclass.
"""
def __init__(self):
strategies[self.name] = self
最后一条语句如何工作?
答案 0 :(得分:3)
EngineStrategy
是一种抽象类。如果尝试搜索此类的用法,则只会从该类中找到继承。
所以self.name
实际上来自cls.name
(类变量),就像下面的PlainEngineStrategy
一样:
class PlainEngineStrategy(DefaultEngineStrategy): """Strategy for configuring a regular Engine.""" name = 'plain' engine_cls = base.Engine
这允许将所有策略存储在strategies
字典中,其结构为:{'stategy_name': strategy_class}
与sqlalchemy
注释有关:官方回购的镜像现在位于https://github.com/sqlalchemy/sqlalchemy