Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!feeder2.cambriumusenet.nl!feed.tweaknews.nl!62.179.104.142.MISMATCH!amsnews11.chello.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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; 'method,': 0.07; 'snippet': 0.07; 'subject:object': 0.07; 'attribute.': 0.09; 'foo': 0.09; 'global,': 0.09; 'storing': 0.09; 'def': 0.13; 'cc:addr:python- list': 0.15; 'mon,': 0.15; 'attribute;': 0.16; 'globals.': 0.16; '\xa0def': 0.16; 'wrote:': 0.16; 'instance': 0.18; 'convert': 0.19; 'jan': 0.19; 'cheers,': 0.20; 'trying': 0.20; 'cc:no real name:2**0': 0.21; 'tells': 0.21; 'header:In-Reply-To:1': 0.22; 'obviously': 0.23; 'assigning': 0.23; 'received:74.125.82.174': 0.24; 'suspect': 0.24; 'cc:2**0': 0.25; '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; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.29; 'second': 0.29; 'class': 0.29; '(and': 0.29; 'definition': 0.30; 'pretty': 0.30; 'actually': 0.32; 'actual': 0.32; 'object': 0.33; 'there': 0.33; 'too': 0.34; 'rather': 0.34; 'certain': 0.34; 'received:74.125.82': 0.34; 'probably': 0.35; 'post': 0.36; 'class.': 0.36; 'minimal': 0.36; 'reference': 0.37; 'but': 0.37; 'received:74.125': 0.37; 'received:google.com': 0.37; 'another': 0.37; 'using': 0.37; 'skip:_ 10': 0.37; 'getting': 0.37; 'should': 0.38; 'skip:\xa0 10': 0.38; "what's": 0.39; 'either': 0.39; 'subject:: ': 0.39; 'might': 0.40; 'more': 0.61; 'here': 0.64; 'details': 0.65; 'heavy': 0.71; '12:44': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=Fu8Sf5Nfro8ekNNKCRUfo2yFwQbZPC5b/UcchREnVVI=; b=F34H7phvQemsctiJ86WD6vEA3nA0GDu6nvHlizMf5c/8XSn/btig2JWmqAuXoiX2tJ 1H7f+722GOowNB9ebZgj7kXu3F0jsRZBxWBxWLmgT2BaP013S7WJg1+1Vip6NxLUJfOj xqeSFf2I+gBwEHL/p45hZG1R8RulMqlRz2Piw= MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Mon, 23 Jan 2012 13:09:38 -0700 Subject: Re: Using an object inside a class To: Jonno Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1327349410 news.xs4all.nl 6976 [2001:888:2000:d::a6]:35311 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19280 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 mor= e > clean OOP. Then you probably should not be using 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 objec= t > 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 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. > Obviously I want the object to be available throughout the class (I left = out > the self.object =3D 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". Cheers, Ian