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


Groups > comp.lang.python > #68165

Closure/method definition question for Python 2.7

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=139a8af39=Gerard.Brunick@constellation.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.06; '__init__': 0.09; 'moreover,': 0.09; 'raises': 0.09; 'subject:2.7': 0.09; 'subject:method': 0.09; 'subject:question': 0.10; 'python': 0.11; 'def': 0.12; 'assume': 0.14; 'question.': 0.14; '__init__,': 0.16; 'defined,': 0.16; 'defined.': 0.16; 'nameerror:': 0.16; 'scope,': 0.16; 'self.y': 0.16; 'variable': 0.18; 'to:name:python-list@python.org': 0.22; 'print': 0.22; 'why.': 0.24; 'fine': 0.24; '---': 0.24; 'subject:/': 0.26; 'code:': 0.26; 'point': 0.28; 'function': 0.29; 'modified,': 0.31; 'anyone': 0.31; 'class': 0.32; 'regular': 0.32; 'skip:_ 10': 0.34; 'could': 0.34; 'but': 0.35; 'really': 0.36; 'charset:us-ascii': 0.36; 'thanks': 0.36; 'should': 0.36; 'example,': 0.37; 'clear': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'explain': 0.39; 'delete': 0.39; 'to:addr:python.org': 0.39; 'sender.': 0.60; 'information,': 0.61; 'simply': 0.61; 'information': 0.63; 'name': 0.63; 'skip:n 10': 0.64; 'close': 0.67; 'addressee.': 0.81; '2.7.': 0.84
X-IronPort-AV E=Sophos;i="4.97,625,1389762000"; d="scan'208";a="91255124"
From "Brunick, Gerard:(Constellation)" <Gerard.Brunick@constellation.com>
To "python-list@python.org" <python-list@python.org>
Subject Closure/method definition question for Python 2.7
Thread-Topic Closure/method definition question for Python 2.7
Thread-Index Ac88hAvod8Va6I2eSQWa6MbrWfWDmQ==
Date Mon, 10 Mar 2014 17:27:45 +0000
Accept-Language en-US
Content-Language en-US
X-MS-Has-Attach
X-MS-TNEF-Correlator
x-originating-ip [10.106.230.16]
Content-Type text/plain; charset="us-ascii"
MIME-Version 1.0
X-CFilter-Loop Reflected
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8014.1394472539.18130.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1394472539 news.xs4all.nl 2975 [2001:888:2000:d::a6]:54324
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:68165

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

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

csiph-web