<br><br><div class="gmail_quote">On Mon, Jan 23, 2012 at 2:09 PM, Ian Kelly <span dir="ltr"><<a href="mailto:ian.g.kelly@gmail.com">ian.g.kelly@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On Mon, Jan 23, 2012 at 12:44 PM, Jonno <<a href="mailto:jonnojohnson@gmail.com">jonnojohnson@gmail.com</a>> wrote:<br>
> I have a pretty complicated bit of code that I'm trying to convert to more<br>
> clean OOP.<br>
<br>
</div>Then you probably should not be using globals.<br></blockquote><div><br></div><div>I'm trying to rewrite the whole thing to get rid of my globals. </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div class="im"><br>
> Without getting too heavy into the details I have an object which I am<br>
> trying to make available inside another class. The reference to the object<br>
> is rather long and convoluted but what I find is that within my class<br>
> definition this works:<br>
><br>
> class Class1:<br>
>     def __init__(self):<br>
><br>
>     def method1(self):<br>
>          foo.bar.object<br>
><br>
> But this tells me "global name foo is not defined":<br>
><br>
> class Class1:<br>
>      def __init__(self):<br>
>            foo.bar.object<br>
<br>
</div>Where is foo actually stored?  Is it in fact a global, or is it<br>
somewhere else?  Please post the actual code.  I suspect that what's<br>
going on here is that you're assigning foo somewhere inside method1<br>
and so it is actually a local variable to that method, but there is no<br>
way to know that for certain from the minimal snippet provided.<br>
<div class="im"><br></div></blockquote><div>The whole code is complex but here is where I define foo and bar:</div><div><div><br></div><div>class MyApp(wx.App):</div><div>    def OnInit(self):</div><div>        self.bar = MyFrame(None, -1, 'App Name')</div>
<div>        self.bar.Show(True)</div><div>        return True</div><div>        </div><div>foo = MyApp(0)</div><div>app.MainLoop()</div></div><div><br></div><div>There is nothing inside method1 except the foo.bar.object reference.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
> Obviously I want the object to be available throughout the class (I left out<br>
> the self.object = etc for simplicity).<br>
<br>
</div>Do you mean that you want the same object to be available to all<br>
instances of Class1, or that you just want the object to be available<br>
to all methods within a single instance (and other instances might<br>
access other objects)?  In the first case, I would recommend storing<br>
foo in a class attribute; in the second case, an instance attribute.<br>
Either way, it would then be accessed simply as "self.foo".<br></blockquote><div><br></div><div>Either way would work but the main issue is I can't seem to use foo or foo.bar or foo.bar.object anywhere in __init__ or even before that in the main class area.</div>
</div>