Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of passing "globals()" as web.py fvars?

Tags:

web.py

web.application accepts an undocumented fvars argument to which the web.py tutorial passes globals() like so:

import web

class index:
    def GET(self):
        return "Hello, world!"

urls = (
  '/', 'index'
)

if __name__ == "__main__": 
    app = web.application(urls, globals())

I've seen at least one application that passes locals(). What is this variable used for, and why would you want to pass it locals() or globals()?

like image 720
André Caron Avatar asked Feb 26 '13 21:02

André Caron


1 Answers

It's used by application.handle() (which in turn calls application._delegate()) to convert the handler class from a string to the class object itself. Source code here.

For example, in your code snippet above, urls = ('/', 'index') is the URL-to-class-string mapping. So web.application needs your globals() dict to be able to look up the string 'index' and get the class itself.

I actually think this is a somewhat non-Pythonic design. Why not just pass in the class directly? I think web.py supports that approach too. However, I believe the classes were done as strings so autoreload is simpler. The autoreload code uses fvars heavily.

Re locals(): at the module level, locals() doesn't really make sense, but it returns the same dictionary as globals(), which is why that would work.

like image 100
Ben Hoyt Avatar answered Nov 16 '22 03:11

Ben Hoyt