Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81319
| From | Ned Batchelder <ned@nedbatchelder.com> |
|---|---|
| Subject | Re: I have no class |
| Date | 2014-11-22 22:14 -0500 |
| References | <e7h27ahs9rjaunu6d91opif62vbr2jefl9@4ax.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.16156.1416712474.18130.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-22 21:47 -0500
Re: I have no class random832@fastmail.us - 2014-11-22 22:08 -0500
Re: I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-22 22:16 -0500
Re: I have no class Rustom Mody <rustompmody@gmail.com> - 2014-11-22 19:09 -0800
Re: I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-22 22:18 -0500
Re: I have no class Ned Batchelder <ned@nedbatchelder.com> - 2014-11-22 22:14 -0500
Re: I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-22 22:35 -0500
Re: I have no class Joel Goldstick <joel.goldstick@gmail.com> - 2014-11-22 22:52 -0500
Re: I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-22 23:15 -0500
Re: I have no class Chris Angelico <rosuav@gmail.com> - 2014-11-23 17:00 +1100
Re: I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-23 05:52 -0500
Re: I have no class Dave Angel <davea@davea.name> - 2014-11-23 10:16 -0500
Re: I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-23 10:54 -0500
Re: I have no class Dave Angel <davea@davea.name> - 2014-11-23 11:14 -0500
Re: I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-23 11:49 -0500
Re: I have no class Chris Angelico <rosuav@gmail.com> - 2014-11-24 03:59 +1100
Re: I have no class Rustom Mody <rustompmody@gmail.com> - 2014-11-23 09:02 -0800
Re: I have no class Steven D'Aprano <steve@pearwood.info> - 2014-11-24 04:21 +0000
Re: I have no class Dave Angel <davea@davea.name> - 2014-11-24 00:11 -0500
Re: I have no class Chris Angelico <rosuav@gmail.com> - 2014-11-24 16:11 +1100
Re: I have no class Dan Sommers <dan@tombstonezero.net> - 2014-11-24 13:44 +0000
Re: I have no class Chris Angelico <rosuav@gmail.com> - 2014-11-25 00:48 +1100
Re: I have no class Dave Angel <davea@davea.name> - 2014-11-24 00:20 -0500
Re: I have no class Chris Angelico <rosuav@gmail.com> - 2014-11-24 16:21 +1100
Re: I have no class Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-24 13:47 -0500
Re: I have no class Rustom Mody <rustompmody@gmail.com> - 2014-11-22 19:55 -0800
Re: I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-22 23:19 -0500
Re: I have no class Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-11-23 15:35 +0000
csiph-web