On Tue, Jul 5, 2011 at 8:47 AM, Joshua Smith <joshuaesm...@charter.net>wrote:

> Thanks for that clarification.  I'm sure there's a reason for this
> asymmetry (must declare globals to write them, but not to read them), but
> it's really weird.
>

Since Python doesn't require variable declaration at all, there's no way for
it to know if "foo = 3" is intended to create a local or a global without an
explicit declaration. If it assumed a local unless a global already existed,
creating a global in your module could unexpectedly change the behavior of a
function that happened to use the same name for a local variable.

-Nick Johnson


>
> On Jul 1, 2011, at 6:12 PM, Geoffrey Spear wrote:
>
> >
> >
> > On Jun 29, 1:40 pm, Joshua Smith <joshuaesm...@charter.net> wrote:
> >> I have this code in one of my apps:
> >>
> >> townCache = {}
> >> def getTown(id):
> >>  if not id in townCache:
> >>    townCache[id] = TownModel.get_by_id(id)
> >>  return townCache[id]
> >>
> >> Is this thread safe?  I think it is, because the worst that happens is
> the assignment happens redundantly with the same data.
> >>
> >> Random other question: Why don't I have to say "global townCache" at the
> top of that function?
> >
> > You can't *assign* to a global variable in another scope without the
> > global keyword; however, townCache is the global name here, not
> > townCache[id].
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> "Google App Engine" group.
> > To post to this group, send email to google-appengine@googlegroups.com.
> > To unsubscribe from this group, send email to
> google-appengine+unsubscr...@googlegroups.com.
> > For more options, visit this group at
> http://groups.google.com/group/google-appengine?hl=en.
> >
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine" group.
> To post to this group, send email to google-appengine@googlegroups.com.
> To unsubscribe from this group, send email to
> google-appengine+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-appengine?hl=en.
>
>


-- 
Nick Johnson, Developer Programs Engineer, App Engine

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

Reply via email to