Dynamic, Generic Namespaces in Python Are a Pain(blog.eatonphil.com)
blog.eatonphil.com
Dynamic, Generic Namespaces in Python Are a Pain
http://blog.eatonphil.com/2015/05/30/dynamic-generic-namespaces-in-python-are-a-pain/
3 comments
What's the complaint? That Python doesn't have anonymous functions? Sure, that would be nice, but his example would work fine if he declared the function with a temporary name first.
>>> for page in ["index", "about", "contact"]:
... def _(p=page):
... print("Hello, I'm", p)
... globals()[page] = _
...
>>> index()
Hello, I'm index
>>> about()
Hello, I'm about
>>> contact()
Hello, I'm contactNot sure what the intent of this post is. Yes, Python indeed isn't Lisp.
First, there is a limitation of Flask itself, not Python. Flask uses the name of the function to allow you to retrieve the url of a specific endpoint with url_for.
So technically lambda would not either but you can trick by defining a local function and changing the name of it or using a class. Here is a working example of what you were trying to do: https://gist.github.com/Lothiraldan/87bf5fc9fba604ececa0.
Good news, if you want this genericity, you could use pluggable views from flask http://flask.pocoo.org/docs/0.10/views/.
And finally you can dynamically inject something into your global namespace using this format: globals()[page] = endpoint but against it will not works due to internal flask limitation not Python one.
So yes, Flask design is simple, maybe too much for your utilization and Python is flawed but just like every god damned language.