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: 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 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: <2qn27adjq412n9nff9kv5jh25d1ekkbncp@4ax.com> <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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On 11/23/2014 10:54 AM, Seymore4Head wrote: > On Sun, 23 Nov 2014 10:16:28 -0500, Dave Angel > wrote: > >> On 11/23/2014 05:52 AM, Seymore4Head wrote: >>> On Sun, 23 Nov 2014 17:00:08 +1100, Chris Angelico >>> 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