Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50696
| References | <51E41A5C.7060903@nottheoilrig.com> |
|---|---|
| Date | 2013-07-16 02:05 +1000 |
| Subject | Re: Is this a bug? |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4736.1373904362.3114.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Is this a bug? Chris Angelico <rosuav@gmail.com> - 2013-07-16 02:05 +1000
csiph-web