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


Groups > comp.lang.python > #19289

Re: Using an object inside a class

References <CAGz2ECYwdnB2ZoTo-DXHhO1_MzdjWeYB+nxeNJ+xivPa8N-tVA@mail.gmail.com> <jfkfn3$92a$1@dough.gmane.org> <CAGz2ECYB=__eKdhdbgWDzRY2DSW4ZcFzuobQCMJFPogV0hcOJA@mail.gmail.com>
Date 2012-01-23 16:40 -0500
Subject Re: Using an object inside a class
From Benjamin Kaplan <benjamin.kaplan@case.edu>
Newsgroups comp.lang.python
Message-ID <mailman.4988.1327354823.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Jan 23, 2012 at 4:22 PM, Jonno <jonnojohnson@gmail.com> wrote:
>
>
> On Mon, Jan 23, 2012 at 2:25 PM, Terry Reedy <tjreedy@udel.edu> 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:
>>>     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.
>>
> 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 = MyApp() happens.
> Is there a good reference on the order that things happen in python when a
> single script is run?
>

"foo = 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 = app, bar = frame,
> object = graph_panel). I'd welcome all suggestions to reorganize it.
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

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


Thread

Re: Using an object inside a class Benjamin Kaplan <benjamin.kaplan@case.edu> - 2012-01-23 16:40 -0500

csiph-web