[Tutor] OOP question

Ben Ganzfried ben.ganzfried at gmail.com
Tue Jan 18 18:25:59 CET 2011


Hey guys,

I'm trying to get a version of Roulette working and I had a quick question.
Here is my code:

class Outcome:

    def __init__(self, name, odds):
        self.name = name
        self.odds = odds

    def winAmount(self, amount):
        return odds*amount

    def __eq__(self, other):
        if (self == other):
            return True
        else:
            return False

    def __ne__(self, other):
        if (self != other):
            return True
        else:
            return False

    def __str__(self):
        return "%s (%d:1)" % ( self.name, self.odds )

Now whenever I try to call the winAmount method, I get the following error:
NameError: global name 'odds' is not defined

I'm wondering the following: I had thought that by initializing "odds" in
the first initialization method that "odds" should then be accessible to all
the other methods in the class.  This does not seem to be the case.  So what
is the ideal fix?  On the one hand I still need to initialize "odds",
right?  But if "odds" is within the initialization method, how else can it
be accessed by the other methods?

Thanks so much!

Ben
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110118/cd66a8aa/attachment.html>


More information about the Tutor mailing list