-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use env vars from within functions #22
Comments
Hey there! So, I dug in a little and found that the first request fails, but the second doesn't. The first one fails because the In other words, And yes, this is counter-intuitive because you wouldn't run into this limitation if you pasted this code into the Python interpreter and ran it. I'm not sure if there's a way to change the code that creates the ###env
base_url = 'http://localhost:8080'
session_id = 'myid'
def apicall(path):
return "{}{}?jsessid={}".format(base_url, path, session_id)
def _apicall(path):
return "http://localhost:8080{}?jsessid=myid".format(path)
###env
# Profile
get(apicall("/api/foo"))
get(_apicall("/api/foo")) |
So, this works just fine. ###env
def caller(path):
def call():
return "http://localhost:8080{}?jsessid=myid".format(path)
return call
###env
# Profile
get(caller("/api/foo")()) Which means the env parsing code can create closures for functions as long as the variables they're referencing aren't assigned in the "top-level". I'm not sure I can fix this. It requires deeper knowledge of the My suggestion is to do something like the following: ###env
def apicall(path):
return "{}{}?jsessid={}".format('http://localhost:8080', path, 'myid')
###env
get(apicall("/api/foo")) Or, equivalently, but more verbose and weird: ###env
base_url = 'http://localhost:8080'
session_id = 'myid'
def apicall(base_url, path, session_id):
return "{}{}?jsessid={}".format(base_url, path, session_id)
###env
get(apicall(base_url, "/api/foo", session_id)) |
Example:
The call fails because
base_url
andsession_id
are empty in the defined function. Declaring themnonlocal
doesn't help. How can this be accomplished without duplicating the constants?The text was updated successfully, but these errors were encountered: