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


Groups > comp.lang.python > #19283

Re: Using an object inside a class

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'received:verizon.net': 0.07; 'subject:object': 0.07; 'terry': 0.07; 'called.': 0.09; 'foo': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'def': 0.13; '__init__': 0.16; 'reedy': 0.16; 'wrote:': 0.16; 'convert': 0.19; 'jan': 0.19; 'trying': 0.20; 'later': 0.21; 'tells': 0.21; 'header :In-Reply-To:1': 0.22; 'obviously': 0.23; 'defined': 0.24; 'code': 0.25; 'pm,': 0.26; 'function': 0.27; "i'm": 0.27; 'bit': 0.28; 'seem': 0.29; 'class': 0.29; 'definition': 0.30; 'pretty': 0.30; 'header:User-Agent:1': 0.33; 'to:addr:python-list': 0.33; 'object': 0.33; 'too': 0.34; 'rather': 0.34; 'purely': 0.34; 'header:X-Complaints-To:1': 0.34; 'class.': 0.36; 'reference': 0.37; 'but': 0.37; 'received:org': 0.37; 'another': 0.37; 'skip:_ 10': 0.37; 'getting': 0.37; 'references': 0.38; 'some': 0.38; 'why': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'more': 0.61; 'presented': 0.62; 'details': 0.65; 'relevant': 0.70; 'heavy': 0.71; 'details.': 0.71; "'foo'": 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Using an object inside a class
Date Mon, 23 Jan 2012 15:25:03 -0500
References <CAGz2ECYwdnB2ZoTo-DXHhO1_MzdjWeYB+nxeNJ+xivPa8N-tVA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-74-109-121-73.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0
In-Reply-To <CAGz2ECYwdnB2ZoTo-DXHhO1_MzdjWeYB+nxeNJ+xivPa8N-tVA@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4979.1327350322.27778.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1327350322 news.xs4all.nl 6911 [2001:888:2000:d::a6]:39425
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19283

Show key headers only | View raw


On 1/23/2012 2:44 PM, Jonno wrote:
> I have a pretty complicated bit of code that I'm trying to convert to
> more clean OOP.
>
> Without getting too heavy into the details I have an object which I am
> trying to make available inside another class. The reference to the
> object is rather long and convoluted but what I find is that within my
> class definition this works:
>
> class Class1:
>      def __init__(self):
>
>      def method1(self):
>           foo.bar.object
>
> But this tells me "global name foo is not defined":
>
> class Class1:
>       def __init__(self):
>             foo.bar.object
>
> Obviously I want the object to be available throughout the class (I left
> out the self.object = etc for simplicity).

Perhaps you left out some relevant details.

> Any ideas why I can reference foo inside the method but not in __init__?

References inside functions are resolved when the function is called. So 
purely from what you have presented above, it would seem that 'foo' is 
defined between the call to __init__ and a later call to method1.

-- 
Terry Jan Reedy

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


Thread

Re: Using an object inside a class Terry Reedy <tjreedy@udel.edu> - 2012-01-23 15:25 -0500

csiph-web