On Sat, 2009-03-07 at 13:42 -0800, Rex wrote:
> I'm reading the documentation on sessions (http://
> docs.djangoproject.com/en/dev/topics/http/sessions/?from=olddocs) and
> saw the following example:
> 
> def post_comment(request, new_comment):
>     if request.session.get('has_commented', False):
>         return HttpResponse("You've already commented.")
>     c = comments.Comment(comment=new_comment)
>     c.save()
>     request.session['has_commented'] = True
>     return HttpResponse('Thanks for your comment!')
> 
> "request" seems to be a local variable, so I would think that setting
> 
> request.session['has_commented'] = True
> 
> would have no effect, since that variable is not used again (e.g. not
> returned by the function).

Sure, "request" is a local variable, but since Python uses
pass-by-reference, it is a reference to an object that is also
referenced by other variables in other scopes (including the session
middleware).

So by assigning to request.session, all the other variables referring to
that object will see the updates.

Cheers,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to