I have no class

Ned Batchelder ned at nedbatchelder.com
Sun Nov 23 04:14:21 CET 2014

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

Ned Batchelder, http://nedbatchelder.com

More information about the Python-list mailing list