Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81381

Re: I have no class

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'attribute': 0.07; 'class,': 0.07; 'defaults': 0.07; 'indices': 0.07; 'modify': 0.07; 'namespace': 0.09; 'variable,': 0.09; 'def': 0.12; 'random': 0.14; '"key"': 0.16; 'belongs': 0.16; 'dictionary.': 0.16; 'extension,': 0.16; 'extension.': 0.16; 'fine.': 0.16; 'once.': 0.16; 'sequential': 0.16; 'subject:class': 0.16; 'such,': 0.16; 'throw': 0.16; 'tuple': 0.16; 'tuple,': 0.16; 'wrote:': 0.18; 'module': 0.19; '(but': 0.19; "python's": 0.19; '>>>': 0.22; 'rules': 0.22; 'saying': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'class.': 0.26; 'references': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; "doesn't": 0.30; 'moved': 0.30; 'gives': 0.31; 'code': 0.31; '>>>>': 0.31; 'class': 0.32; 'another': 0.32; 'skip:_ 10': 0.34; 'could': 0.34; 'possible.': 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'words,': 0.36; 'useful': 0.36; 'thanks': 0.36; 'should': 0.36; 'list': 0.37; 'thank': 0.38; 'problems': 0.38; 'convention': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'planning': 0.38; 'explain': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'dave': 0.60; 'referred': 0.60; "you're": 0.61; 'first': 0.61; 'making': 0.63; 'name': 0.63; 'skip:1 20': 0.65; 'received:74.208': 0.68; 'helping': 0.70; 'wish': 0.70; 'subject:have': 0.80; 'trial': 0.83; 'all!': 0.84; 'confusing': 0.84; "it'd": 0.84; 'angel': 0.91; 'thing,': 0.91
Date Sun, 23 Nov 2014 11:14:34 -0500
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0
MIME-Version 1.0
To python-list@python.org
Subject Re: I have no class
References <e7h27ahs9rjaunu6d91opif62vbr2jefl9@4ax.com> <mailman.16156.1416712474.18130.python-list@python.org> <l9l27alml8scm4dvf29m2vidcudhh6766o@4ax.com> <mailman.16157.1416714761.18130.python-list@python.org> <2qn27adjq412n9nff9kv5jh25d1ekkbncp@4ax.com> <mailman.16160.1416722412.18130.python-list@python.org> <kre37at1cg1m0k3vcav2ekgohfrs5rb465@4ax.com> <mailman.16185.1416755794.18130.python-list@python.org> <8d047ahkrb4uh4qpq1uk74tel4nfc4s8uv@4ax.com>
In-Reply-To <8d047ahkrb4uh4qpq1uk74tel4nfc4s8uv@4ax.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:OcvBy/yF5VltcLAfnwd73f2HXj+hhbr4lK3dicFQ9xF fj3QFckinbbnJgWzkmsngzmHiPe+YoDDL93m7iVqm0r/JBUcxN 7jfIXO1ZEHaj6LMi4dCueW6GHuLYpUfsbHtW9V0aUwwFAvpAG2 n+gpd5o9KiomskV4ziP+07ZM6Z8Xju1/Ag8EKWz9POVX8RqJiZ +GHulYfZaSsmJku4V/v1Gsf1ovL/B7F5uoZjvLdHP05saMBDpg xudRbEAQ5CNn2skRcX01JU6EXE0ewBQignfKpUsXPkTCjA46tl PQ3Qf+Kch6NTk9E7VU06RPyKnKtXBxyj3VXAudFL/iEsuGTCIC XOwn0w7+w4Ygb8X3zW1U=
X-UI-Out-Filterresults notjunk:1;
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16192.1416759279.18130.python-list@python.org> (permalink)
Lines 73
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1416759279 news.xs4all.nl 2964 [2001:888:2000:d::a6]:42025
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81381

Show key headers only | View raw


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

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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