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


Groups > comp.lang.python > #19283 > unrolled thread

Re: Using an object inside a class

Started byTerry Reedy <tjreedy@udel.edu>
First post2012-01-23 15:25 -0500
Last post2012-01-23 15:25 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Using an object inside a class Terry Reedy <tjreedy@udel.edu> - 2012-01-23 15:25 -0500

#19283 — Re: Using an object inside a class

FromTerry Reedy <tjreedy@udel.edu>
Date2012-01-23 15:25 -0500
SubjectRe: Using an object inside a class
Message-ID<mailman.4979.1327350322.27778.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web