Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #50696 > unrolled thread

Re: Is this a bug?

Started byChris Angelico <rosuav@gmail.com>
First post2013-07-16 02:05 +1000
Last post2013-07-16 02:05 +1000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Is this a bug? Chris Angelico <rosuav@gmail.com> - 2013-07-16 02:05 +1000

#50696 — Re: Is this a bug?

FromChris Angelico <rosuav@gmail.com>
Date2013-07-16 02:05 +1000
SubjectRe: Is this a bug?
Message-ID<mailman.4736.1373904362.3114.python-list@python.org>
On Tue, Jul 16, 2013 at 1:50 AM, Jack Bates <tdhfwh@nottheoilrig.com> wrote:
> Hello,
>
> Is the following code supposed to be an UnboundLocalError?
> Currently it assigns the value 'bar' to the attribute baz.foo
>
>    foo = 'bar'
>    class baz:
>       foo = foo
> --
> http://mail.python.org/mailman/listinfo/python-list

Unless you're creating that class inside a function, it would be
NameError, not UnboundLocalError. Due to the way class scopes work,
it's actually possible and sometimes useful to do this, as it
"snapshots" the current referent of that name. It's like what happens
with default arguments to a function:

foo = 'bar'
def func(foo=foo):
    return foo
foo = 'quux'

print(func())

The newly-defined name isn't "in scope" until its assignment is
complete; until then, the old name is fully in scope.

ChrisA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web