Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!newsfeed10.multikabel.net!multikabel.net!newsfeed20.multikabel.net!news2.euro.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'method,': 0.07; 'snippet': 0.07; 'subject:object': 0.07; 'attribute.': 0.09; 'bind': 0.09; 'foo': 0.09; 'from:addr:python': 0.09; 'global,': 0.09; 'storing': 0.09; 'def': 0.13; 'mon,': 0.15; '__init__': 0.16; 'attribute;': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'globals.': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'occurs,': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'reply-to:addr:python-list': 0.16; 'wrote:': 0.16; 'instance': 0.18; 'rewrite': 0.18; 'convert': 0.19; 'jan': 0.19; 'trying': 0.20; 'tells': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'obviously': 0.23; 'assigning': 0.23; 'reference.': 0.23; 'suspect': 0.24; 'code': 0.25; 'somewhere': 0.25; 'code.': 0.26; 'pm,': 0.26; 'fact': 0.27; "i'm": 0.27; 'variable': 0.27; 'bit': 0.28; 'rid': 0.28; 'seem': 0.29; 'second': 0.29; 'class': 0.29; '(and': 0.29; 'definition': 0.30; '-1,': 0.30; 'exist.': 0.30; 'kelly': 0.30; 'line:': 0.30; 'pretty': 0.30; 'actually': 0.32; 'actual': 0.32; "can't": 0.32; 'header:User-Agent:1': 0.33; 'to:addr:python-list': 0.33; 'object': 0.33; 'there': 0.33; 'received:84': 0.34; 'too': 0.34; 'rather': 0.34; 'certain': 0.34; 'reply-to:addr:python.org': 0.34; 'probably': 0.35; 'issue': 0.35; 'post': 0.36; 'class.': 0.36; 'minimal': 0.36; 'reference': 0.37; 'but': 0.37; 'another': 0.37; 'using': 0.37; 'skip:_ 10': 0.37; 'getting': 0.37; 'should': 0.38; 'except': 0.39; "what's": 0.39; 'either': 0.39; 'define': 0.39; 'subject:: ': 0.39; 'might': 0.40; 'to:addr:python.org': 0.40; 'more': 0.61; 'here': 0.64; 'details': 0.65; 'header:Reply-To:1': 0.71; 'heavy': 0.71; 'reply-to:no real name:2**0': 0.72; "'foo'": 0.84; '12:44': 0.84; 'bar:': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=J8QoHXbS c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=FFsdPcU1_MIA:10 a=7vMG9l4F-74A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=pGLkceISAAAA:8 a=MFq7gIG2rKlQaP6DSWwA:9 a=wPNLvfGTeEIA:10 a=MSl-tDqOz04A:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Mon, 23 Jan 2012 20:58:18 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:9.0) Gecko/20111222 Thunderbird/9.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Using an object inside a class References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org 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: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1327352287 news.xs4all.nl 6914 [2001:888:2000:d::a6]:60245 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19287 On 23/01/2012 20:27, Jonno wrote: > > > On Mon, Jan 23, 2012 at 2:09 PM, Ian Kelly > wrote: > > On Mon, Jan 23, 2012 at 12:44 PM, Jonno > wrote: > > I have a pretty complicated bit of code that I'm trying to > convert to more > > clean OOP. > > Then you probably should not be using globals. > > > I'm trying to rewrite the whole thing to get rid of my globals. > > > > 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 > > Where is foo actually stored? Is it in fact a global, or is it > somewhere else? Please post the actual code. I suspect that what's > going on here is that you're assigning foo somewhere inside method1 > and so it is actually a local variable to that method, but there is no > way to know that for certain from the minimal snippet provided. > > The whole code is complex but here is where I define foo and bar: > > class MyApp(wx.App): > def OnInit(self): > self.bar = MyFrame(None, -1, 'App Name') > self.bar.Show(True) > return True > foo = MyApp(0) > app.MainLoop() > > There is nothing inside method1 except the foo.bar.object reference. > > > Obviously I want the object to be available throughout the class > (I left out > > the self.object = etc for simplicity). > > Do you mean that you want the same object to be available to all > instances of Class1, or that you just want the object to be available > to all methods within a single instance (and other instances might > access other objects)? In the first case, I would recommend storing > foo in a class attribute; in the second case, an instance attribute. > Either way, it would then be accessed simply as "self.foo". > > > Either way would work but the main issue is I can't seem to use foo or > foo.bar or foo.bar.object anywhere in __init__ or even before that in > the main class area. > This line: foo = MyApp(0) will create a 'MyApp' instance and then bind it to the name 'foo'. Until that binding occurs, the name 'foo' doesn't exist.