Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51937
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.datemas.de!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.007 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'assign': 0.07; 'modifying': 0.07; 'iterate': 0.09; 'python': 0.11; 'def': 0.12; 'bug': 0.12; 'random': 0.14; '(note': 0.16; 'blocks': 0.16; 'declaration': 0.16; 'dictionary.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'mapping,': 0.16; 'subject:issue': 0.16; 'subject:simple': 0.16; 'superfluous': 0.16; 'unix-style': 0.16; 'wrote:': 0.18; 'meant': 0.20; 'import': 0.22; 'aug': 0.22; 'putting': 0.22; 'print': 0.22; 'possibly': 0.26; 'this:': 0.26; 'second': 0.26; 'skip:_ 20': 0.27; 'header :In-Reply-To:1': 0.27; 'character': 0.29; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'though.': 0.31; 'types.': 0.31; 'class': 0.32; 'probably': 0.32; 'another': 0.32; 'skip:# 10': 0.33; 'skip:b 30': 0.33; "i'd": 0.34; 'objects': 0.35; 'received:google.com': 0.35; 'doing': 0.36; 'needed': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'enough': 0.39; 'even': 0.60; 'easy': 0.60; 'new': 0.61; 'simply': 0.61; "you're": 0.61; 'name': 0.63; 'map': 0.64; 'more': 0.64; 'here': 0.66; 'fact,': 0.69; 'boys': 0.84; '2013': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=t+hWwBmaeQQdfBBRgE0SRgHDluVYeResOSqVSZvABYA=; b=eHyH9LjPnWHorFkuI2JmPD59sq5pb+eha3XEulfuAK0Z/rs6LLgJyi2WsSeuz8U4dz sMCCo7kiT57CGiTnEkbaucuS1b+FtWfluzdJ9Rk3tpYitDgBnerKQe2Kr4HrTLsDK97s FhzhezJSh1n+n+e2ArB1F2+tTein9GnXP/sTGtpSv31OCjx+VIqX1zD6gWvn+H7qniu9 FmED80cFRom/hnqXCiIyESb/GjRIjyJY6rU9ghIpo+Kr/rQguoQ6ZyKz1B6dna0WjW5P L74j/r12BAKa0nMjC/nX5b7EmXUXOZ/19MOrViSHSwhyWU7TfCUls8La4Z1KIzfAfQsG e7xQ== |
| MIME-Version | 1.0 |
| X-Received | by 10.52.34.40 with SMTP id w8mr4030169vdi.7.1375641573073; Sun, 04 Aug 2013 11:39:33 -0700 (PDT) |
| In-Reply-To | <51fe9b6b$0$1689$e4fe514c@dreader35.news.xs4all.nl> |
| References | <51fe9b6b$0$1689$e4fe514c@dreader35.news.xs4all.nl> |
| Date | Sun, 4 Aug 2013 19:39:33 +0100 |
| Subject | Re: stupid simple scope issue |
| From | Chris Angelico <rosuav@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-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 | <http://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 | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.190.1375641581.1251.python-list@python.org> (permalink) |
| Lines | 72 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1375641581 news.xs4all.nl 16001 [2001:888:2000:d::a6]:51207 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:51937 |
Show key headers only | View raw
On Sun, Aug 4, 2013 at 7:20 PM, JohnD <john@nowhere.com> wrote:
> #~/usr/bin/python
If this is meant to be a Unix-style shebang, the second character
needs to be ! not ~. This has no effect on Python though.
> import random
> class boys:
> state={}
> class boy:
> state={
> 'name':'',
> 'age':''
> }
At no time do you actually instantiate any objects from these types.
In fact, you may as well drop the class blocks and the ".state" usage
and simply use:
boys = {}
boy = {'name':'', 'age':''}
as this will achieve the exact same thing.
> def add_names():
> global boys
The global declaration is needed only if you assign to the name, eg
"boys = <...>" - it's superfluous here.
> for n in names:
> boy.state['name']=n
> boy.state['age']=random.randint(1, 1000)
> boys.state[n]=boy.state
> print boy.state['name'], boy.state['age']
Each time you do this, you're modifying the same 'boy' mapping, then
putting another reference to it in 'boys'. I think possibly what you
want here is to construct a new boy() instance for each one.
> add_names()
>
> for n in boys.state:
> boy.state=boys.state[n]
> print boy.state['name'], boy.state['age']
I'd look at doing it more like this:
class boy:
def __init__(self,name,age):
self.name=name; self.age=age
boys = {}
def add_name(n):
b = boy(n,random.randint(1, 1000))
boys[n] = b
print b.name, b.age
for n in 'a','b','c':
add_name(n)
for n,b in boys.items():
print b.name, b.age
Or possibly even dispense with the boy class altogether and simply use
a dictionary - or simply map a name to an age, since (as you can see
in the final loop) it's easy enough to iterate over the dictionary.
(Note that the code above is untested and probably has an egregious
bug in it somewhere.)
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
stupid simple scope issue JohnD <john@nowhere.com> - 2013-08-04 18:20 +0000
Re: stupid simple scope issue Chris Angelico <rosuav@gmail.com> - 2013-08-04 19:39 +0100
Re: stupid simple scope issue JohnD <john@nowhere.com> - 2013-08-04 19:21 +0000
Re: stupid simple scope issue Chris Angelico <rosuav@gmail.com> - 2013-08-04 21:03 +0100
Re: stupid simple scope issue JohnD <john@nowhere.com> - 2013-08-05 07:21 +0000
csiph-web