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


Groups > comp.lang.python > #68167

Re: Closure/method definition question for Python 2.7

From Marko Rauhamaa <marko@pacujo.net>
Newsgroups comp.lang.python
Subject Re: Closure/method definition question for Python 2.7
Date 2014-03-10 19:36 +0200
Organization A noiseless patient Spider
Message-ID <87vbvmq7l3.fsf@elektro.pacujo.net> (permalink)
References <mailman.8014.1394472539.18130.python-list@python.org>

Show all headers | View raw


"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

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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