简体   繁体   中英

Class scopes in Python Bottle routes

Inside a bottle route I am instantiating a class.

Feasibly, this page may get called simultaneously and need to create simultaneous instances of this class named "newuser" in the function.

I wanted to make sure there won't be conflicts since all instances are assigned the name "newuser" by the function.

I think this is fine since the class is created within the function call and the scope of the class should only be local to the function?

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)

This is indeed fine; the newuser name is entirely local to the test() function scope. The instances will not be shared between calls to that route.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM