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: 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 Subject: Re: Using an object inside a class Date: Mon, 23 Jan 2012 15:25:03 -0500 References: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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