Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!rt.uk.eu.org!nntp-feed.chiark.greenend.org.uk!ewrotcd!news.nosignal.org!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'app,': 0.07; 'subject:object': 0.07; 'terry': 0.07; 'python': 0.08; 'called.': 0.09; 'foo': 0.09; 'initialized': 0.09; 'pointers': 0.09; 'def': 0.13; "hasn't": 0.13; 'mon,': 0.15; '__init__': 0.16; 'did.': 0.16; 'foo.': 0.16; 'frame,': 0.16; 'initializes': 0.16; 'reedy': 0.16; 'stripped': 0.16; '\xa0def': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'exists': 0.18; 'instance': 0.18; 'convert': 0.19; 'jan': 0.19; 'trying': 0.20; 'posting': 0.20; 'later': 0.21; 'tells': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'figure': 0.23; 'obviously': 0.23; 'somehow': 0.23; 'though.': 0.23; 'defined': 0.24; 'code': 0.25; 'pm,': 0.26; 'url:mailman': 0.26; 'function': 0.27; "i'm": 0.27; 'bit': 0.28; 'script': 0.28; 'message-id:@mail.gmail.com': 0.28; 'seem': 0.29; 'class': 0.29; 'definition': 0.30; 'exist.': 0.30; 'oop': 0.30; '(the': 0.30; 'url:listinfo': 0.30; 'pretty': 0.30; 'yet': 0.32; 'it.': 0.33; 'to:addr:python-list': 0.33; 'object': 0.33; 'there': 0.33; 'creates': 0.34; 'received:209.85.212': 0.34; 'too': 0.34; 'rather': 0.34; 'purely': 0.34; 'then.': 0.34; 'running': 0.35; 'things': 0.35; 'finished': 0.35; 'suggestions': 0.35; 'url:python': 0.36; 'class.': 0.36; 'push': 0.36; 'reference': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'another': 0.37; 'skip:_ 10': 0.37; 'getting': 0.37; 'references': 0.38; 'some': 0.38; 'received:209.85': 0.38; 'created': 0.38; 'skip:\xa0 10': 0.38; "i'd": 0.39; 'url:org': 0.39; 'why': 0.39; "what's": 0.39; 'being': 0.39; 'strong': 0.39; 'called': 0.39; 'received:209': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'order': 0.60; 'more': 0.61; 'happen': 0.61; 'presented': 0.62; 'here': 0.64; 'details': 0.65; 'teach': 0.70; 'relevant': 0.70; 'heavy': 0.71; 'details.': 0.71; '"foo"': 0.84; "'foo'": 0.84; 'reorganize': 0.84; 'run?': 0.84 MIME-Version: 1.0 In-Reply-To: References: Date: Mon, 23 Jan 2012 16:40:13 -0500 Subject: Re: Using an object inside a class From: Benjamin Kaplan To: python-list@python.org X-Gm-Message-State: ALoCoQknJ5QehDKUBUYnSEY+E403PruMH/No4sL1mpZ0id78d9HzcCbWZ7u+uEb/ctJ8f+bg0o4q Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Junkmail-Whitelist: YES (by domain whitelist at mpv1.tis.cwru.edu) 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: 78 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1327354823 news.xs4all.nl 6899 [2001:888:2000:d::a6]:40519 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19289 On Mon, Jan 23, 2012 at 4:22 PM, Jonno wrote: > > > On Mon, Jan 23, 2012 at 2:25 PM, Terry Reedy wrote: >> >> 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: >>> =A0 =A0 def __init__(self): >>> >>> =A0 =A0 def method1(self): >>> =A0 =A0 =A0 =A0 =A0foo.bar.object >>> >>> But this tells me "global name foo is not defined": >>> >>> class Class1: >>> =A0 =A0 =A0def __init__(self): >>> =A0 =A0 =A0 =A0 =A0 =A0foo.bar.object >>> >>> Obviously I want the object to be available throughout the class (I lef= t >>> out the self.object =3D etc for simplicity). >> >> >> Perhaps you left out some relevant details. >> > I'm sure I did. Part of the reason I'm not posting the whole code is that > I'm trying to teach myself OOP as part of this process. I want to figure = out > what is wrong as much as possible by myself. I really appreciate the > pointers and suggestions though. > >> >> >>> 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. > > > I have a strong suspicion that this is what's happening. > > Method1 is called on a button push when MainLoop is running so obviously = foo > (the main wx.App) exists by then. > I must have somehow be initializing Class1 before foo =3D MyApp() happens= . > Is there a good reference on the order that things happen in python when = a > single script is run? > "foo =3D MyApp()" creates an instance of MyApp, initializes it, and then binds it to the name foo. If Class1 is being initialized in MyApp.__init__, then the MyApp instance hasn't finished being created yet so the name "foo" doesn't exist. > In the meantime here is my stripped down script (foo =3D app, bar =3D fra= me, > object =3D graph_panel). I'd welcome all suggestions to reorganize it. > > > > -- > http://mail.python.org/mailman/listinfo/python-list >