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


Groups > comp.lang.python > #19284

Re: Using an object inside a class

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!nuzba.szn.dk!pnx.dk!amsnews11.chello.com!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <gherron@digipen.edu>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.011
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'subject:object': 0.07; 'python': 0.08; 'bind': 0.09; 'doing,': 0.09; 'foo': 0.09; 'foo,': 0.09; 'variables.': 0.09; 'am,': 0.12; 'def': 0.13; 'this:': 0.15; '*really*': 0.16; '__init__': 0.16; 'builtins.': 0.16; 'foo.': 0.16; 'wrote:': 0.16; 'convert': 0.19; 'trying': 0.20; 'tells': 0.21; 'header:In-Reply-To:1': 0.22; 'obviously': 0.23; 'defined': 0.24; 'code': 0.25; 'module': 0.26; "i'm": 0.27; 'variable': 0.27; 'bit': 0.28; 'work:': 0.28; 'class': 0.29; 'definition': 0.30; 'fails,': 0.30; 'pretty': 0.30; 'does': 0.32; 'header:User- Agent:1': 0.33; 'to:addr:python-list': 0.33; 'object': 0.33; 'rules': 0.33; 'too': 0.34; 'rather': 0.34; "i'll": 0.35; 'class.': 0.36; 'reference': 0.37; 'but': 0.37; 'another': 0.37; 'skip:_ 10': 0.37; 'could': 0.37; 'getting': 0.37; 'why': 0.39; 'goes': 0.39; 'either': 0.39; 'being': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'more': 0.61; 'your': 0.61; 'full': 0.62; 'fact,': 0.63; 'further': 0.64; 'details': 0.65; 'received:10.10': 0.68; 'heavy': 0.71; 'us,': 0.71; '11:44': 0.84; 'everything.': 0.84
Date Mon, 23 Jan 2012 12:23:25 -0800
From Gary Herron <gherron@digipen.edu>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20111124 Thunderbird/8.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Using an object inside a class
References <CAGz2ECYwdnB2ZoTo-DXHhO1_MzdjWeYB+nxeNJ+xivPa8N-tVA@mail.gmail.com>
In-Reply-To <CAGz2ECYwdnB2ZoTo-DXHhO1_MzdjWeYB+nxeNJ+xivPa8N-tVA@mail.gmail.com>
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
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.4981.1327350608.27778.python-list@python.org> (permalink)
Lines 72
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1327350608 news.xs4all.nl 6915 [2001:888:2000:d::a6]:38790
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19284

Show key headers only | View raw


On 01/23/2012 11:44 AM, 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).
>
> Any ideas why I can reference foo inside the method but not in __init__?
>
>

You're not telling us everything.  In fact, with the code you gave us, 
neither method1 nor __init__ will work correctly, because you have not 
defined foo *anywhere*.

Without knowledge of what you are *really* doing, I'll say this:  Both 
method1 and __init__ are methods of Class1, and both have the same rules 
for looking up variables.   If either method binds a value to foo, then 
your code may access it:

class Class1:
      def __init__(self):
            foo = whatever # Local to this method
            foo.bar.object

If the method does not bind it, then Python will look in the class for 
foo.  This could work

class Class1:
      foo = whatever # Available to all instances
      def __init__(self):
            foo.bar.object

If that fails, Python will look in the globals, so this could work:

foo = whatever # Available across the full module
class Class1:
      def __init__(self):
            foo.bar.object

Python goes on one further level when searching for a variable -- that 
being the builtins.







-- 
Gary Herron, PhD.
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

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


Thread

Re: Using an object inside a class Gary Herron <gherron@digipen.edu> - 2012-01-23 12:23 -0800

csiph-web