I have no class

Dave Angel davea at davea.name
Sun Nov 23 10:16:28 EST 2014


On 11/23/2014 05:52 AM, Seymore4Head wrote:
> On Sun, 23 Nov 2014 17:00:08 +1100, Chris Angelico <rosuav at gmail.com>
> wrote:
>
>>
>> 1) Python's namespacing rules mean that 'key' is a part of the RPS
>> class, and can be referred to as 'self.key' or as 'RPS.key'
>> 2) Use of 'self.key' for the textual form of the throw is shadowing
>> the first of those reference names (but it's a poor name anyway)
>> 3) A list would work just as well as a dictionary here, since your
>> indices are sequential and start from zero
>> 4) There's another function in the random module which can do both of
>> your steps at once.
>>
>> Thank you for helping us help you help us all!
>>
>> ChrisA
>
> I wish I could explain my problems so precisely.  I will try to do
> better in the future.
> Thanks
>
> I will also try this using a list instead of a dictionary.
>

And please avoid the shadowing.  It's confusing whenever the same name 
is used for a class attribute and an instance attribute of the same 
class.  it can be useful for defaults and such, but not here.

  class RPS:
      KEY_TABLE={0:"rock", 1:"paper",2:"scissors"};
   or
      KEY_TABLE= ("rock", "paper", "scissors")
      def __init__(self):
          self.throw=random.randrange(3)
          self.key=RPS.KEY_TABLE[self.throw]

Note I also capitalized the tuple name, which is a convention saying we 
don't intend to modify it.

-- 
DaveA



More information about the Python-list mailing list