Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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):
        pass

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

class Baz():
    def __init__(self):
        pass

    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    

os.system("clear")      
foo = Foo()
foo.account(5, 11)
foo.testy(100, 100)
bar = Bar()
bar.zippy(10, 100)
baz = Baz()
baz.cracky(1000, 1)
share|improve this question
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? – Useless Aug 21 '12 at 13:41
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. – dwstein Aug 21 '12 at 16:07
Oh yeah, my mistake! Those references are to the global foo declared after it's used, which is ... unusual. – Useless Aug 21 '12 at 16:21
Fair enough. Please rip up the code and tell me where I can do beter. – dwstein Aug 21 '12 at 16:42
Also, the fact that a piece of code runs without throwing exceptions is not the same thing as code that has the correct behavior ;) – Matt Phillips Aug 21 '12 at 23:04

2 Answers

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:

share|improve this answer

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):
     self.players.append(p)

  def removePlayer(self,p):
     self.players.remove(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()
game.addPlayer(p1)
game.addPlayer(p2)

p2.increaseScore(1)
p1.increaseScore(5)
game.printScore()

game.removePlayer(p1)
game.removePlayer(p2)
game.printScore()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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