[Tutor] OOP question

Nick Stinemates nstinemates at gmail.com
Wed Jan 19 18:16:58 CET 2011


If you want to test if one outcome is equal to another, you probably want to
know if

The name of the outcome is the same
The odds of the outcome is the same

So, you'd implement it like so

class Outcome:

   def __eq__(self, other):
       if self.name == other.name and self.odds == other.odds:
           return True
       return False


Make sense?

Nick

On Wed, Jan 19, 2011 at 9:07 AM, Ben Ganzfried <ben.ganzfried at gmail.com>wrote:

> yeah I was actually pretty confused about those methods as well.  I mean I
> get the general idea that we are testing to see if two outcomes are the same
> (hence a winning bet)...when you say expand on them, what do you mean
> exactly?
>
> Thanks again, really much appreciated!
>
>
> On Tue, Jan 18, 2011 at 4:31 PM, Nick Stinemates <nstinemates at gmail.com>wrote:
>
>> You may also want to look in to the __eq__ and __ne__ methods. You're just
>> re-implementing standard behavior (so redefining them is useless.) You'll
>> probably want to expand on them to be more concise.
>>
>>
>> On Tue, Jan 18, 2011 at 10:00 AM, Ben Ganzfried <ben.ganzfried at gmail.com>wrote:
>>
>>> Thanks, Nick!
>>>
>>>
>>> On Tue, Jan 18, 2011 at 12:52 PM, Nick Stinemates <nstinemates at gmail.com
>>> > wrote:
>>>
>>>> Updated inline. Check the updated definiton of winAmount.
>>>>
>>>> Nick
>>>>
>>>> On Tue, Jan 18, 2011 at 9:25 AM, Ben Ganzfried <ben.ganzfried at gmail.com
>>>> > wrote:
>>>>
>>>>> 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 self.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
>>>>>
>>>>> _______________________________________________
>>>>> Tutor maillist  -  Tutor at python.org
>>>>> To unsubscribe or change subscription options:
>>>>> http://mail.python.org/mailman/listinfo/tutor
>>>>>
>>>>>
>>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110119/80b004a5/attachment-0001.html>


More information about the Tutor mailing list