Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81315 > unrolled thread
| Started by | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| First post | 2014-11-22 21:47 -0500 |
| Last post | 2014-11-23 15:35 +0000 |
| Articles | 20 on this page of 28 — 11 participants |
Back to article view | Back to comp.lang.python
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
Page 1 of 2 [1] 2 Next page →
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-11-22 21:47 -0500 |
| Subject | I have no class |
| Message-ID | <e7h27ahs9rjaunu6d91opif62vbr2jefl9@4ax.com> |
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
print ("a ",a.throw)
print ("b ",b.throw)
if a.throw == b.throw:
print("Tie")
elif (a.throw - b.throw)%3==1:
print("a Wins")
else:
print("b Wins")
---------
I tried:
class RPS:
def __init__(self):
self.throw=random.randrange(3)
AttributeError: type object 'RPS' has no attribute 'throw'
[toc] | [next] | [standalone]
| From | random832@fastmail.us |
|---|---|
| Date | 2014-11-22 22:08 -0500 |
| Message-ID | <mailman.16154.1416712114.18130.python-list@python.org> |
| In reply to | #81315 |
On Sat, Nov 22, 2014, at 21:47, 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
>
> print ("a ",a.throw)
> print ("b ",b.throw)
> if a.throw == b.throw:
> print("Tie")
> elif (a.throw - b.throw)%3==1:
> print("a Wins")
> else:
> print("b Wins")
>
> ---------
> I tried:
>
> class RPS:
> def __init__(self):
> self.throw=random.randrange(3)
>
> AttributeError: type object 'RPS' has no attribute 'throw'
You need that class, and also a=RPS(); b=RPS().
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-11-22 22:16 -0500 |
| Message-ID | <8bk27a9q1itgksu294esj0epsrmip25ou0@4ax.com> |
| In reply to | #81316 |
On Sat, 22 Nov 2014 22:08:31 -0500, random832@fastmail.us wrote:
>
>
>On Sat, Nov 22, 2014, at 21:47, 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
>>
>> print ("a ",a.throw)
>> print ("b ",b.throw)
>> if a.throw == b.throw:
>> print("Tie")
>> elif (a.throw - b.throw)%3==1:
>> print("a Wins")
>> else:
>> print("b Wins")
>>
>> ---------
>> I tried:
>>
>> class RPS:
>> def __init__(self):
>> self.throw=random.randrange(3)
>>
>> AttributeError: type object 'RPS' has no attribute 'throw'
>
>You need that class, and also a=RPS(); b=RPS().
That works.
Thanks
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2014-11-22 19:09 -0800 |
| Message-ID | <6497f0f1-9a99-4766-84b0-b9522637f971@googlegroups.com> |
| In reply to | #81315 |
On Sunday, November 23, 2014 8:17:24 AM UTC+5:30, 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
>
> print ("a ",a.throw)
> print ("b ",b.throw)
> if a.throw == b.throw:
> print("Tie")
> elif (a.throw - b.throw)%3==1:
> print("a Wins")
> else:
> print("b Wins")
>
> ---------
> I tried:
>
> class RPS:
> def __init__(self):
> self.throw=random.randrange(3)
>
> AttributeError: type object 'RPS' has no attribute 'throw'
Works as far as I can see:
import random
class RPS:
def __init__(self):
self.throw=random.randrange(3)
-----------------
>>> a = RPS()
>>> a.throw
0
>>> a = RPS()
>>> a.throw
2
>>> a = RPS()
>>> a.throw
2
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-11-22 22:18 -0500 |
| Message-ID | <jck27a5or64rvc62v05dg12u1s3akdsh1j@4ax.com> |
| In reply to | #81317 |
On Sat, 22 Nov 2014 19:09:27 -0800 (PST), Rustom Mody
<rustompmody@gmail.com> wrote:
>On Sunday, November 23, 2014 8:17:24 AM UTC+5:30, 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
>>
>> print ("a ",a.throw)
>> print ("b ",b.throw)
>> if a.throw == b.throw:
>> print("Tie")
>> elif (a.throw - b.throw)%3==1:
>> print("a Wins")
>> else:
>> print("b Wins")
>>
>> ---------
>> I tried:
>>
>> class RPS:
>> def __init__(self):
>> self.throw=random.randrange(3)
>>
>> AttributeError: type object 'RPS' has no attribute 'throw'
>
>Works as far as I can see:
>
>import random
>class RPS:
> def __init__(self):
> self.throw=random.randrange(3)
>-----------------
>>>> a = RPS()
>>>> a.throw
>0
>>>> a = RPS()
>>>> a.throw
>2
>>>> a = RPS()
>>>> a.throw
>2
It works when you remember to include the ()
I am still making rookie mistakes.
Thanks
[toc] | [prev] | [next] | [standalone]
| From | Ned Batchelder <ned@nedbatchelder.com> |
|---|---|
| Date | 2014-11-22 22:14 -0500 |
| Message-ID | <mailman.16156.1416712474.18130.python-list@python.org> |
| In reply to | #81315 |
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
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-11-22 22:35 -0500 |
| Message-ID | <l9l27alml8scm4dvf29m2vidcudhh6766o@4ax.com> |
| In reply to | #81319 |
On Sat, 22 Nov 2014 22:14:21 -0500, Ned Batchelder
<ned@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]
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2014-11-22 22:52 -0500 |
| Message-ID | <mailman.16157.1416714761.18130.python-list@python.org> |
| In reply to | #81322 |
On Sat, Nov 22, 2014 at 10:35 PM, Seymore4Head
<Seymore4Head@hotmail.invalid> wrote:
> On Sat, 22 Nov 2014 22:14:21 -0500, Ned Batchelder
> <ned@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
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-11-22 23:15 -0500 |
| Message-ID | <2qn27adjq412n9nff9kv5jh25d1ekkbncp@4ax.com> |
| In reply to | #81323 |
On Sat, 22 Nov 2014 22:52:33 -0500, Joel Goldstick
<joel.goldstick@gmail.com> wrote:
>On Sat, Nov 22, 2014 at 10:35 PM, Seymore4Head
><Seymore4Head@hotmail.invalid> wrote:
>> On Sat, 22 Nov 2014 22:14:21 -0500, Ned Batchelder
>> <ned@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?
Traceback (most recent call last):
File "C:\Documents and Settings\Administrator\Desktop\rps.py", line
7, in <module>
a=RPS()
File "C:\Documents and Settings\Administrator\Desktop\rps.py", line
6, in __init__
self.key=key[self.throw]
NameError: name 'key' is not defined
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-11-23 17:00 +1100 |
| Message-ID | <mailman.16160.1416722412.18130.python-list@python.org> |
| In reply to | #81326 |
On Sun, Nov 23, 2014 at 3:15 PM, Seymore4Head
<Seymore4Head@hotmail.invalid> wrote:
> Traceback (most recent call last):
> File "C:\Documents and Settings\Administrator\Desktop\rps.py", line
> 7, in <module>
> a=RPS()
> File "C:\Documents and Settings\Administrator\Desktop\rps.py", line
> 6, in __init__
> self.key=key[self.throw]
> NameError: name 'key' is not defined
This information is far more helpful than "it is broke". Note how it
says that the name 'key' is not defined? You're expecting that it
would be defined, because you defined it a few lines earlier. So
here's a more useful way to ask the question:
-- cut --
I'm trying to use a dictionary to translate throw numbers into
descriptions, but even though the dictionary has been defined, Python
is telling me it hasn't.
import random
class RPS:
key={0:"rock", 1:"paper",2:"scissors"};
def __init__(self):
self.throw=random.randrange(3)
self.key=key[self.throw]
a=RPS()
Traceback (most recent call last):
File "C:\Documents and Settings\Administrator\Desktop\rps.py", line
7, in <module>
a=RPS()
File "C:\Documents and Settings\Administrator\Desktop\rps.py", line
6, in __init__
self.key=key[self.throw]
NameError: name 'key' is not defined
It seems to me that 'key' is defined on line 3. Can anyone explain
what's happening here? Thanks!
-- cut --
Note that I've posted the *entire* code of your script (it's a
reconstruction, but I suspect it's fairly close; there might be more
code after that, but it's not affecting anything), and included the
traceback in the first message, rather than waiting for someone to ask
for it.
Armed with this information, someone can tell you:
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
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-11-23 05:52 -0500 |
| Message-ID | <kre37at1cg1m0k3vcav2ekgohfrs5rb465@4ax.com> |
| In reply to | #81331 |
On Sun, 23 Nov 2014 17:00:08 +1100, Chris Angelico <rosuav@gmail.com>
wrote:
>On Sun, Nov 23, 2014 at 3:15 PM, Seymore4Head
><Seymore4Head@hotmail.invalid> wrote:
>> Traceback (most recent call last):
>> File "C:\Documents and Settings\Administrator\Desktop\rps.py", line
>> 7, in <module>
>> a=RPS()
>> File "C:\Documents and Settings\Administrator\Desktop\rps.py", line
>> 6, in __init__
>> self.key=key[self.throw]
>> NameError: name 'key' is not defined
>
>This information is far more helpful than "it is broke". Note how it
>says that the name 'key' is not defined? You're expecting that it
>would be defined, because you defined it a few lines earlier. So
>here's a more useful way to ask the question:
>
>-- cut --
>I'm trying to use a dictionary to translate throw numbers into
>descriptions, but even though the dictionary has been defined, Python
>is telling me it hasn't.
>
>import random
>class RPS:
> key={0:"rock", 1:"paper",2:"scissors"};
> def __init__(self):
> self.throw=random.randrange(3)
> self.key=key[self.throw]
>a=RPS()
>
>Traceback (most recent call last):
> File "C:\Documents and Settings\Administrator\Desktop\rps.py", line
>7, in <module>
> a=RPS()
> File "C:\Documents and Settings\Administrator\Desktop\rps.py", line
>6, in __init__
> self.key=key[self.throw]
>NameError: name 'key' is not defined
>
>It seems to me that 'key' is defined on line 3. Can anyone explain
>what's happening here? Thanks!
>-- cut --
>
>Note that I've posted the *entire* code of your script (it's a
>reconstruction, but I suspect it's fairly close; there might be more
>code after that, but it's not affecting anything), and included the
>traceback in the first message, rather than waiting for someone to ask
>for it.
>
>Armed with this information, someone can tell you:
>
>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.
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-11-23 10:16 -0500 |
| Message-ID | <mailman.16185.1416755794.18130.python-list@python.org> |
| In reply to | #81359 |
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.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-11-23 10:54 -0500 |
| Message-ID | <8d047ahkrb4uh4qpq1uk74tel4nfc4s8uv@4ax.com> |
| In reply to | #81369 |
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.
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]
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-11-23 11:14 -0500 |
| Message-ID | <mailman.16192.1416759279.18130.python-list@python.org> |
| In reply to | #81379 |
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.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-11-23 11:49 -0500 |
| Message-ID | <up347a14v7v65meph33k53kas3jkqgmoo5@4ax.com> |
| In reply to | #81381 |
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
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-11-24 03:59 +1100 |
| Message-ID | <mailman.16199.1416761966.18130.python-list@python.org> |
| In reply to | #81388 |
On Mon, Nov 24, 2014 at 3:49 AM, Seymore4Head <Seymore4Head@hotmail.invalid> wrote: > 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. That's because Python doesn't really do the whole "infinitely-nested scopes" thing that you see in C-like languages. (That kind of scoping doesn't really work without declared variables, which is completely against the Python model.) So you have to be a bit more explicit about where you want to reference things from. What you can do is use 'self' instead of the class name; that's often the cleanest way to do it. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2014-11-23 09:02 -0800 |
| Message-ID | <66b13454-3503-4111-bf8e-58ce79ef00aa@googlegroups.com> |
| In reply to | #81388 |
On Sunday, November 23, 2014 10:20:05 PM UTC+5:30, Seymore4Head wrote: > 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. Yes that is natural. Python is a bit odd in the OO-world in that it prioritizes "Explicit is better than implicit" over convenience. Notice that you use self.throw where in most other OOP languages you would use just throw. Since the whole point of making 'key' (the table) a class variable as against an instance variable is that it is not attached to self. So where is it attached? Answer follows from "Explicit is better than implicit"
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2014-11-24 04:21 +0000 |
| Message-ID | <5472b24c$0$12979$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #81391 |
On Sun, 23 Nov 2014 09:02:57 -0800, Rustom Mody wrote:
> Python is a bit odd in the OO-world in that it prioritizes "Explicit is
> better than implicit" over convenience.
>
> Notice that you use self.throw where in most other OOP languages you
> would use just throw.
I don't think that is correct. I think that most OOP languages are like
Python, and use a special variable to reference the current instance:
"self" is used by:
Smalltalk, Python, Perl, Objective-C, Swift, Ruby, OCAML
"this" is used by:
C++, C#, Java, D, Javascript, F#, Cobra, PHP, Powershell
"me" is used by:
ABAP Objects, Visual Basic
"Current" is used by:
Eiffel
http://en.wikipedia.org/wiki/This_(computer_programming)
http://en.wikipedia.org/wiki/Comparison_of_programming_languages_(object-
oriented_programming)#Special_variables
In some of these languages, the use of "this/self/me" is optional, but
I'm not aware of *any* OOP language where there is no named reference to
the current object at all.
In any case, Python is not unusual in this regard.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-11-24 00:11 -0500 |
| Message-ID | <mailman.16225.1416805907.18130.python-list@python.org> |
| In reply to | #81422 |
On 11/23/2014 11:21 PM, Steven D'Aprano wrote:
> In some of these languages, the use of "this/self/me" is optional, but
> I'm not aware of *any* OOP language where there is no named reference to
> the current object at all.
>
The case I found astounding in C++ was in the initializer list where the
line
value:value
would assume that the first one was this->value, and the second was a
local named value (usually an argument to the constructor).
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-11-24 16:11 +1100 |
| Message-ID | <mailman.16224.1416805895.18130.python-list@python.org> |
| In reply to | #81422 |
On Mon, Nov 24, 2014 at 3:21 PM, Steven D'Aprano <steve@pearwood.info> wrote: > On Sun, 23 Nov 2014 09:02:57 -0800, Rustom Mody wrote: > >> Python is a bit odd in the OO-world in that it prioritizes "Explicit is >> better than implicit" over convenience. >> >> Notice that you use self.throw where in most other OOP languages you >> would use just throw. > > I don't think that is correct. I think that most OOP languages are like > Python, and use a special variable to reference the current instance: > > In some of these languages, the use of "this/self/me" is optional, but > I'm not aware of *any* OOP language where there is no named reference to > the current object at all. I believe his point is that Python, unlike every other language he can think of, requires "self.x" instead of just "x". Every language needs a way to say "current object", but not every language needs you to say that for every member reference. C++, Pike, and Java let you short-hand that. (They're all deriving from the same syntactic style anyway.) JavaScript doesn't, I believe, although its variable scoping rules are some of the most insane I've ever met, so there might be a way to shortcut it. If your experience of OO is mainly from C++/Java family languages, then yes, Python will seem odd. ChrisA
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web