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


Groups > comp.lang.python > #19294

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> <CALwzidmYEc9q1ve7u1Vgu-AAVJ6ZBrF36hCZ_=fndvuAFgXMfg@mail.gmail.com> <CAGz2ECYY9bpNrY3aOS95dwi_vC4p0KHQtERLF51pFHFTaKv6Tg@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2012-01-23 15:20 -0700
Subject Re: Using an object inside a class
Newsgroups comp.lang.python
Message-ID <mailman.4992.1327357292.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Jan 23, 2012 at 2:52 PM, Jonno <jonnojohnson@gmail.com> wrote:
> On Mon, Jan 23, 2012 at 3:42 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>>
>> Exactly.  The line "app = MyApp(0)" creates a MyApp instance and then
>> assigns it to "app".  As part of the MyApp creation process, it
>> creates a MyFrame, which creates a Tab, which creates a Class1, which
>> attempts to reference "app".  All of this happens before that
>> "MyApp(0)" call has returned, so the result of that call has not
>> actually been assigned to "app" yet.
>>
>> I suggest using wx.GetApp() instead.
>>
> That totally makes sense. However I'm not sure I understand your suggestion
> how to use wx.GetApp()
> Isn't the wxApp still not created before Class1 is instantiated so I still
> can't call wx.GetApp() in __init__ of Class1 can I?

The App object is created and the wx framework already knows about it.
 It's just not assigned to the app global yet, and the OnInit call has
not completed yet.  See:

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
>>> class MyApp(wx.App):
...     def OnInit(self):
...         print "wx.GetApp() =", wx.GetApp()
...         print "app =", app
...         return True
...
>>> app = MyApp(0)
wx.GetApp() = <__main__.MyApp; proxy of <Swig Object of type 'wxPyApp
*' at 0x18d8fc0> >
app =
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py",
line 7823, in __init__
    self._BootstrapApp()
  File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py",
line 7420, in _BootstrapApp
    return _core_.PyApp__BootstrapApp(*args, **kwargs)
  File "<stdin>", line 4, in OnInit
NameError: global name 'app' is not defined

Cheers,
Ian

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


Thread

Re: Using an object inside a class Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-23 15:20 -0700

csiph-web