V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
julyclyde
V2EX  ›  Flask

Flask(__name__)这个参数到底起了什么作用?

  •  
  •   julyclyde ·
    julyclyde · 2015-08-14 14:18:44 +08:00 · 5211 次点击
    这是一个创建于 3387 天前的主题,其中的信息可能已经有所发展或是发生改变。
    之前文档里说计算相对路径的时候会用到这个,但我试了试好像随便给个什么参数去初始化Flask对象都是一样的结果啊……

    除了作为一个属性用来区分多个Flask对象以外,这个初始化参数还能在对象的行为上“起什么作用”?
    2 条回复    2015-08-15 18:24:45 +08:00
    niseceric
        1
    niseceric  
       2015-08-14 23:14:56 +08:00   ❤️ 1
    你可以阅读源码 flask/app.py 里面的Flask类

    这只是个应用程序的名字罢了:

    .. admonition:: About the First Parameter
    The idea of the first parameter is to give Flask an idea of what
    belongs to your application. This name is used to find resources
    on the filesystem, can be used by extensions to improve debugging
    information and a lot more.
    So it's important what you provide there. If you are using a single
    module, `__name__` is always the correct value. If you however are
    using a package, it's usually recommended to hardcode the name of
    your package there.
    For example if your application is defined in :file:`yourapplication/app.py`
    you should create it with one of the two versions below::
    app = Flask('yourapplication')
    app = Flask(__name__.split('.')[0])
    Why is that? The application will work even with `__name__`, thanks
    to how resources are looked up. However it will make debugging more
    painful. Certain extensions can make assumptions based on the
    import name of your application. For example the Flask-SQLAlchemy
    extension will look for the code in your application that triggered
    an SQL query in debug mode. If the import name is not properly set
    up, that debugging information is lost. (For example it would only
    pick up SQL queries in `yourapplication.app` and not
    `yourapplication.views.frontend`)

    __name__黑科技主要是下面这个:

    @locked_cached_property
    def name(self):
    """The name of the application. This is usually the import name
    with the difference that it's guessed from the run file if the
    import name is main. This name is used as a display name when
    Flask needs the name of the application. It can be set and overridden
    to change the value.
    .. versionadded:: 0.8
    """
    if self.import_name == '__main__':
    fn = getattr(sys.modules['__main__'], '__file__', None)
    if fn is None:
    return '__main__'
    return os.path.splitext(os.path.basename(fn))[0]
    return self.import_name
    julyclyde
        2
    julyclyde  
    OP
       2015-08-15 18:24:45 +08:00
    @niseceric 就是看了This name is used to find resources on the filesystem
    但没发现到底怎么use的

    def name(self)这个是返回其name吧,注释没看懂,只看懂as a display name了。
    求教
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2801 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 15:06 · PVG 23:06 · LAX 07:06 · JFK 10:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.