I have no class

Joel Goldstick joel.goldstick at gmail.com
Sat Nov 22 22:52:33 EST 2014


On Sat, Nov 22, 2014 at 10:35 PM, Seymore4Head
<Seymore4Head at hotmail.invalid> wrote:
> On Sat, 22 Nov 2014 22:14:21 -0500, Ned Batchelder
> <ned at nedbatchelder.com> wrote:
>
>>On 11/22/14 9:47 PM, Seymore4Head wrote:
>>> What do I need to do to make a and b have different values?
>>> import random
>>> class RPS:
>>>      throw=random.randrange(3)
>>> a=RPS
>>> b=RPS
>>
>>This simply makes a and b into other names for the class RPS. To
>>instantiate a class to make an object, you have to call it:
>>
>>     a = RPS()
>>     b = RPS()
>>
>>> ---------
>>> I tried:
>>>
>>> class RPS:
>>>      def __init__(self):
>>>          self.throw=random.randrange(3)
>>>
>>> AttributeError: type object 'RPS' has no attribute 'throw'
>>
>>This is the right way to define the class (almost: in Py 2, you should
>>derive from object). Once you make an object from it, you will have what
>>you want:
>>
>>     class RPS(object):
>>         def __init__(self):
>>             self.throw = random.randrange(3)
>>
>>     a = RPS()
>>     b = RPS()
>>     print a.throw
>>     print b.throw
>
> Now I am trying to add a dictionary, but it is broke too.
>
> How do I fix:
> class RPS:
>     key={0:"rock", 1:"paper",2:"scissors"};
>     def __init__(self):
>         self.throw=random.randrange(3)
>         self.key=key[self.throw]
> --
> https://mail.python.org/mailman/listinfo/python-list

can you please show traceback and output?

-- 
Joel Goldstick
http://joelgoldstick.com



More information about the Python-list mailing list