[Tutor] Scope and elegance

James Newton jnewton at fuelindustries.com
Mon Jan 7 21:07:06 CET 2008


Hi Python Practicioners!

I'm trying to come to terms with how Python handles the scope of
objects.  In particular, I'd like to create an instance of one class and
make it available inside instances of other classes.  Something like:


# Test 1-------------------------------------
# I want a "global" instance of this class...
class Foo(object):
    def doFoo(self):
        return "foo"


# So that I can call one of its functions from another instance 
class Bar(object):
    def doBar(self):
        # I'd like be able to call a "global" object, but this fails:
        # global name 'foo_instance' is not defined
	  return foo_instance.doFoo()
    

def main():
    foo_instance = Foo()
    bar_instance = Bar()
    print bar_instance.doBar()


if __name__ == '__main__': main()
# --------------------------------------------


I know that I can pass a pointer to foo_instance as a parameter to the
Bar() call.  For example, this will work:


# Test 2 -------------------------------------
class Foo(object):
    def doFoo(self):
        return "foo"



class Bar(object):
    def doBar(self, aFooInstance):
         return aFooInstance.doFoo()
    

def main():
    foo_instance = Foo()
    bar_instance = Bar()
    print bar_instance.doBar(foo_instance)


if __name__ == '__main__': main()
# --------------------------------------------


I know that I can also create a global instance outside the main()
function.  This will also work:


# Test 3 -------------------------------------
class Foo(object):
    def doFoo(self):
        return "foo"


class Bar(object):
    def doBar(self):
         return foo_instance.doFoo()

    
foo_instance = Foo()


def main():
    bar_instance = Bar()
    print bar_instance.doBar()


if __name__ == '__main__': main()
# --------------------------------------------


However, this assumes that the Foo instance() is independent of anything
that needs to be done inside the main() function. 


To put the question in context: in the Snakes and Ladders game that I am
making, I have a board divided into 100 squares.  I want a Counter
instance to know which position on the Board it should move to.  I may
have several Counter instances, but only one Board instance.  Each
Counter knows the index number of the square it should move to.  The
Board instance knows how to convert that index number into a screen
position.  I want the Counter instances to be able to ask the Board
instance for that screen position.


Is it unPythonic of me to:
a) Want to create the Board instance inside the main() function
AND
b) Want the Board instance to be globally available so that
   Counters and other objects can talk to it directly?

Or is there just a declaration that I have overlooked that can make an
instance created inside a function visible inside any instance?


I am not so much concerned in getting this to work (I can already do
that); I am more concerned with understanding how to treat this in the
most Pythonesque way.


Thanks in advance for your insights,

James 


More information about the Tutor mailing list