Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81388
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: I have no class |
| Date | 2014-11-23 11:49 -0500 |
| Message-ID | <up347a14v7v65meph33k53kas3jkqgmoo5@4ax.com> (permalink) |
| References | (4 earlier) <mailman.16160.1416722412.18130.python-list@python.org> <kre37at1cg1m0k3vcav2ekgohfrs5rb465@4ax.com> <mailman.16185.1416755794.18130.python-list@python.org> <8d047ahkrb4uh4qpq1uk74tel4nfc4s8uv@4ax.com> <mailman.16192.1416759279.18130.python-list@python.org> |
On Sun, 23 Nov 2014 11:14:34 -0500, Dave Angel <davea@davea.name>
wrote:
>On 11/23/2014 10:54 AM, Seymore4Head wrote:
>> On Sun, 23 Nov 2014 10:16:28 -0500, Dave Angel <davea@davea.name>
>> wrote:
>>
>>> On 11/23/2014 05:52 AM, Seymore4Head wrote:
>>>> On Sun, 23 Nov 2014 17:00:08 +1100, Chris Angelico <rosuav@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.
>>
>> I just learned by trial and error if I moved "key" out of the class, I
>> could still use it in the class without having to give it a RPS
>> extension.
>
>It's not an extension, it's a namespace qualifier. That's a good thing,
>to constrain a particular name to as narrow a scope as possible. In
>other words, instead of making it a global variable, put it in the
>namespace that gives it meaning.
>
>>
>> I also changed it to a list and it works fine.
>>
>> Thanks
>>
>> key =["rock", "paper", "scissors"]
>> class RPS:
>> def __init__(self):
>> self.throw=random.randrange(3)
>> self.key=key[self.throw]
>>
>
>But it belongs in the class, doesn't it? If other code wants to use it,
>it'd be better if all references were of the form RPS.KEY_TABLE. And
>it should be a tuple, not a list, as you're not planning to change it.
Like I have said, most of the stuff I am doing is still trial and
error. Having to specify RPS to use it inside the class seemed wrong
to me.
I haven't read enough about classes yet to know what the correct way
is yet.
I will leave it in.
Thanks
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