I wrote the following code to test a score-keeping class. The idea was to keep score of a game such that the score was kept across multiple classes and methods.

I'm looking for any input on being more efficient, more 'pythonic' and/or just better.

import os

class Foo():
    def __init__(self):
        self.stored_end = 0

    def account(self, a, b):
        c = float(a) + b
        print a
        print b
        print c
        self.stored_end = c
        print self.stored_end

    def testy(self, q, v):
        print "\n"
        print " _ " * 10
        z = float(q) + v
        print self.stored_end   
        self.stored_end = self.stored_end + z
        print " _ " * 10
        print self.stored_end

class Bar():
    def __init__(self):

    def zippy(self, a, b):
        print " _ " * 10
        print "this is zippy"
        foo.testy(a, b)

class Baz():
    def __init__(self):

    def cracky(self, g, m):
        y = g + m
        print " _ " * 10
        print "calling stored_end"
        foo.stored_end = foo.stored_end + y
        print " _ " * 10
        print "this is cracky"
        print "y = %r" % y
        print foo.stored_end    

foo = Foo()
foo.account(5, 11)
foo.testy(100, 100)
bar = Bar()
bar.zippy(10, 100)
baz = Baz()
baz.cracky(1000, 1)
  • \$\begingroup\$ Like Matt, I can't really tell what you're trying to achieve: this code is obviously incomplete (there are several references to a foo that is never declared), and seems far from minimal. Could you show some runnable code which demonstrates (as simply as possible) what you need to do? \$\endgroup\$
    – Useless
    Aug 21, 2012 at 13:41
  • \$\begingroup\$ This code runs when I use it. Maybe you need to scroll up to seethe bottom of the code? If not, I think you might be talking over my head. \$\endgroup\$ Aug 21, 2012 at 16:07
  • \$\begingroup\$ Oh yeah, my mistake! Those references are to the global foo declared after it's used, which is ... unusual. \$\endgroup\$
    – Useless
    Aug 21, 2012 at 16:21
  • \$\begingroup\$ Fair enough. Please rip up the code and tell me where I can do beter. \$\endgroup\$ Aug 21, 2012 at 16:42
  • \$\begingroup\$ Also, the fact that a piece of code runs without throwing exceptions is not the same thing as code that has the correct behavior ;) \$\endgroup\$ Aug 21, 2012 at 23:04

2 Answers 2


I think what you may be looking for is the Borg design pattern. It's meant for a single class to maintain state between multiple instances. Not exactly what you're looking for but you could modify it to also maintain state across multiple classes, perhaps by specifying a global for shared state:

## {{{ http://code.activestate.com/recipes/66531/ (r2)
class Borg:
    __shared_state = {}
    def __init__(self):
        self.__dict__ = self.__shared_state
    # and whatever else you want in your class -- that's all!
## end of http://code.activestate.com/recipes/66531/ }}}

Here are some links to various code examples of this, non chronological:


I'm confused on what you are trying to achieve with the code example above, but in general it's usually a bad idea to modify the instance fields of a class from outside of that class. So updating foo.stored_end from another class directly (versus creating a method in foo that takes the new value as a parameter) is usually a bad idea. Here's an example that uses a Game class to keep track of Players and calculate a score.

class Player():
  def __init__(self):
     self.score = 0

  def increaseScore(self,x):
     self.score += x

  def decreaseScore(self,x):
     self.score -= x

class Game():
  def __init__(self):
     self.players = []

  def addPlayer(self,p):

  def removePlayer(self,p):

  def printScore(self):
     score = 0
     for p in self.players:
        score += p.score
     print "Current Score {}".format(score)

p1 = Player()
p2 = Player()

game = Game()



Not the answer you're looking for? Browse other questions tagged or ask your own question.