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


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

Closure/method definition question for Python 2.7

Started by"Brunick, Gerard:(Constellation)" <Gerard.Brunick@constellation.com>
First post2014-03-10 17:27 +0000
Last post2014-03-11 13:20 +0000
Articles 3 — 3 participants

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


Contents

  Closure/method definition question for Python 2.7 "Brunick, Gerard:(Constellation)" <Gerard.Brunick@constellation.com> - 2014-03-10 17:27 +0000
    Re: Closure/method definition question for Python 2.7 Marko Rauhamaa <marko@pacujo.net> - 2014-03-10 19:36 +0200
      Re: Closure/method definition question for Python 2.7 Neil Cerutti <neilc@norwich.edu> - 2014-03-11 13:20 +0000

#68165 — Closure/method definition question for Python 2.7

From"Brunick, Gerard:(Constellation)" <Gerard.Brunick@constellation.com>
Date2014-03-10 17:27 +0000
SubjectClosure/method definition question for Python 2.7
Message-ID<mailman.8014.1394472539.18130.python-list@python.org>
The following code:

---
class Test(object):
    x = 10

    def __init__(self):
        self.y = x

t = Test()
---

raises

NameError: global name 'x' is not defined.

in Python 2.7.  I don't understand why.  I would assume that when __init__ is being defined, it is just a regular old function and x is a variable in an outer scope, so the function __init__ would close over the variable x.  Moreover, the variable x is not being modified, so this should be O.K.  For example, the following is fine (if nonsensical):

---
def outer():
    x = 10

    def __init__(self):
        self.y = x

    return __init__

t = outer()
print t
---

Can anyone explain this behavior?  It is clear that you could simply use self.x to access the variable x inside of __init__, but this isn't really the point of the question.  

Thanks in advance,
Gerard

This e-mail and any attachments are confidential, may contain legal,
professional or other privileged information, and are intended solely for the
addressee. If you are not the intended recipient, do not use the information
in this e-mail in any way, delete this e-mail and notify the sender. -EXCIP

[toc] | [next] | [standalone]


#68167

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-03-10 19:36 +0200
Message-ID<87vbvmq7l3.fsf@elektro.pacujo.net>
In reply to#68165
"Brunick, Gerard:(Constellation)" <Gerard.Brunick@constellation.com>:

> class Test(object):
>     x = 10
>
>     def __init__(self):
>         self.y = x
>
> t = Test()
> ---
>
> raises
>
> NameError: global name 'x' is not defined.

In the snippet, x is neither local to __init__() nor global to the
module. It is in the class scope. You can refer to it in one of two
ways:

   Test.x

or:

   self.x


Marko

[toc] | [prev] | [next] | [standalone]


#68216

FromNeil Cerutti <neilc@norwich.edu>
Date2014-03-11 13:20 +0000
Message-ID<mailman.8046.1394544066.18130.python-list@python.org>
In reply to#68167
On 2014-03-10, Marko Rauhamaa <marko@pacujo.net> wrote:
> "Brunick, Gerard:(Constellation)" <Gerard.Brunick@constellation.com>:
>
>> class Test(object):
>>     x = 10
>>
>>     def __init__(self):
>>         self.y = x
>>
>> t = Test()
>> ---
>>
>> raises
>>
>> NameError: global name 'x' is not defined.
>
> In the snippet, x is neither local to __init__() nor global to
> the module. It is in the class scope. You can refer to it in
> one of two ways:
>
>    Test.x
>
> or:
>
>    self.x

The latter will work only to read the class variable. If you
assign to self.x you'll create a new instance variable that hides
the class variable.

-- 
Neil Cerutti

[toc] | [prev] | [standalone]


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


csiph-web