【问题标题】:Class scopes in Python Bottle routesPython Bottle 路由中的类作用域
【发布时间】:2013-11-18 12:22:33
【问题描述】:

在一个瓶子路径中,我正在实例化一个类。

这个页面可能会被同时调用,并且需要在函数中创建名为“newuser”的类的同时实例。

我想确保不会发生冲突,因为函数为所有实例分配了名称“newuser”。

我认为这很好,因为类是在函数调用中创建的,并且类的范围应该只在函数本地?

from bottle import route, run

class user:
    def __init__(self,id, name):
        self.id = id
        self.name = name
        #Do some stuff that takes a while.



@route('/user/<id>/<name>', method = 'POST')
def test():
    newuser = user(id, name)


run(host='localhost', port=8080, debug=True)

【问题讨论】:

    标签: python class oop scope bottle


    【解决方案1】:

    这确实很好; newuser 名称完全是 test() 函数范围的本地名称。实例不会在对该路由的调用之间共享。

    【讨论】:

    • 太好了,只是想确认一下。谢谢。
    猜你喜欢
    • 2013-05-26
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多