Dirk Calloway
Dirk Calloway

Reputation: 2619

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)

Upvotes: 1

Views: 697

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121784

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.

Upvotes: 2

Related Questions