gives me an error message:
class logger:
session = web.ctx.session #this line
doesn't give me an error message:
class create:
def GET(self):
# loggedout()
session = web.ctx.s开发者_StackOverflow社区ession #this line
form = self.createform()
return render.create(form)
Why?
web.ctx
can't be used in that scope. It's a thread-local object that web.py
initializes before it calls GET/POST/etc.
and gets discarded afterwards.
class logger:
print('Hi')
prints Hi
. Statements under a class definition gets run at definition time.
A function definition like this one:
def GET(self):
# loggedout()
session = web.ctx.session #this line
form = self.createform()
return render.create(form)
is also a statement. It creates the function object which is named GET
. But the code inside the function does not get run until the GET
method is called.
This is why you get an error message in the first case, but not the second.
精彩评论