On Jun 29, 1:28 am, Oliver Christen <[email protected]> wrote:
> hello
>
> Im using pylons 0.9.7 in a project, but when I try to use the
> theoretically global object "response", I get the following error
> message in logs:
>
> Error - <type 'exceptions.NameError'>: global name 'response' is not
> defined
>
> the error happens at this line in my code:
>
> response.headers['content-type'] = 'application/x-json'
>
> do I have to explicitly import some module or something ?

Yes:

    from pylons import response

Unlike, for example, PHP or JavaScript, there's not much that's
globally global (so to speak) in Python, except built-ins like `len`.
In JavaScript, if you define a var at the window level, that var is
available everywhere, which is great source of confusion and errors.
In Python, there's no analogous super-global namespace--you always
have to import the symbols you need, and this a good thing IMO.

With regard to `pylons.response` and friends, these are global in the
sense that there's a single object that you import and use in all your
application code, but they're not global in the sense of existing in a
top-level global scope.

Also, a tiny shortcut: you can do `response.content_type` instead of
`response.headers['content-type']` (and there are similar attributes
for other HTTP headers).

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to