Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17722
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.chainon-marquant.org!news-transit.tcx.org.uk!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rolf@roce.be> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.032 |
| X-Spam-Evidence | '*H*': 0.94; '*S*': 0.00; 'parameter': 0.05; 'initialize': 0.07; 'versions.': 0.07; 'x-mailer:evolution 2.28.3': 0.07; 'referenced': 0.09; 'am,': 0.12; 'def': 0.13; 'alex23': 0.16; 'arrays.': 0.16; 'definition.': 0.16; 'ok!': 0.16; 'pythonic': 0.16; 'subject:=': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'modified': 0.18; 'appropriate': 0.22; 'dec': 0.22; '(or': 0.22; 'header:In-Reply-To:1': 0.22; 'figure': 0.23; 'object,': 0.24; "i'm": 0.26; 'function': 0.27; 'yield': 0.29; 'problem': 0.29; '22,': 0.30; 'object.': 0.30; 'python3': 0.30; 'threads': 0.30; "i've": 0.31; 'list': 0.32; 'this.': 0.33; 'there': 0.33; 'object': 0.33; 'points': 0.34; 'to:addr:python-list': 0.34; 'someone': 0.34; 'eric': 0.34; 'things': 0.34; 'assignment': 0.34; 'integer': 0.34; 'list.': 0.35; 'example,': 0.37; 'variables': 0.37; 'but': 0.37; 'think': 0.37; 'using': 0.38; 'several': 0.38; "i'd": 0.39; 'extend': 0.39; 'why': 0.39; "it's": 0.40; 'to:addr:python.org': 0.40; 'difference': 0.40; 'more': 0.61; 'your': 0.61; 'header:Received:6': 0.61; 'afraid': 0.63; 'dangerous': 0.64; '10.': 0.68; '[3]': 0.73; '16:50': 0.84; 'labels': 0.84; 'received:10.86': 0.84; 'received:72.35': 0.84; 'received:72.35.23': 0.84; 'received:electric.net': 0.84; 'received:mailanyone.net': 0.84; 'so:': 0.84 |
| Subject | Re: what does 'a=b=c=[]' do |
| From | Rolf Camps <rolf@roce.be> |
| To | python-list@python.org |
| In-Reply-To | <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> |
| References | <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> |
| Content-Type | text/plain; charset="UTF-8" |
| Date | Thu, 22 Dec 2011 09:51:54 +0100 |
| Mime-Version | 1.0 |
| X-Mailer | Evolution 2.28.3 |
| Content-Transfer-Encoding | 7bit |
| X-Outbound-IP | 10.86.5.47 |
| X-Env-From | rolf@roce.be |
| X-PolicySMART | 1564782 |
| X-Virus-Status | Scanned by VirusSMART (c) |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| 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.3971.1324550706.27778.python-list@python.org> (permalink) |
| Lines | 81 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1324550706 news.xs4all.nl 6949 [2001:888:2000:d::a6]:34857 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:17722 |
Show key headers only | View raw
alex23 schreef op wo 21-12-2011 om 16:50 [-0800]:
> On Dec 22, 8:25 am, Eric <einazaki...@yahoo.com> wrote:
> > This surprises me, can someone tell me why it shouldn't? I figure if
> > I want to create and initialize three scalars the just do "a=b=c=7",
> > for example, so why not extend it to arrays.
>
> The thing to remember is that everything is an object, and that it's
> better to think of variables as labels on an object.
>
> So: a=b=c=7 means that _one_ integer object with the value of 7 can be
> referenced using any of the labels a, b or c. x=y=z=[] means that
> _one_ empty list can be referenced using x, y or z.
>
> The difference is that the value of a number object _cannot be
> changed_ ('immutable') while a list can be modified to add or remove
> items ('mutable'). a=10 just reassigns the label a to an integer
> object of value 10. x.append("foo") _modifies_ the list referred to by
> x, which is the same list known as y & z.
>
> > Also, is there a more pythonic way to do "x=[], y=[], z=[]"?
>
> I'd say that _is_ the most pythonic way, it's very obvious in its
> intent (or would be with appropriate names). If it bothers you that
> much:
>
> def listgen(count, default=[]):
> for _ in xrange(count):
> yield default[:]
>
> x, y, z = listgen(3)
>
I'm afraid it's dangerous to encourage the use of '[]' as assignment to
a parameter in a function definition. If you use the function several
times 'default' always points to the same list.
>>> def return_list(list_ = []):
>>> return list_
>>> a_list = return_list()
>>> a_list
[]
>>> a_list.append(3)
>>> a_list
[3]
>>> b_list = return_list()
>>> b_list
>>> [3] # !!??
>>> def return_list():
>>> return []
>>> a_list = return_list()
>>> a_list
[]
>>> a_list.append(3)
>>> a_list
[3]
>>> b_list = return_list()
>>> b_list
>>> [] # OK!
I only use python3 so I don't know how these things work in other
versions.
No problem in your function since you yield a copy, but I've already
seen long threads about this.
I would change your function to (Python3.x):
def empty_lists(count):
for _ in range(count):
yield []
Regards,
Rolf
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
what does 'a=b=c=[]' do Eric <einazaki668@yahoo.com> - 2011-12-21 14:25 -0800
Re: what does 'a=b=c=[]' do Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-12-21 18:20 -0500
Re: what does 'a=b=c=[]' do Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-21 23:48 +0000
Re: what does 'a=b=c=[]' do Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-12-24 19:41 +0100
Re: what does 'a=b=c=[]' do Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-25 13:16 +0000
Re: what does 'a=b=c=[]' do Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-12-24 19:45 +0100
Re: what does 'a=b=c=[]' do Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-21 23:44 +0000
Re: what does 'a=b=c=[]' do Eric <einazaki668@yahoo.com> - 2011-12-22 20:27 -0800
Re: what does 'a=b=c=[]' do alex23 <wuwei23@gmail.com> - 2011-12-21 16:50 -0800
Re: what does 'a=b=c=[]' do Rolf Camps <rolf@roce.be> - 2011-12-22 09:51 +0100
Re: what does 'a=b=c=[]' do alex23 <wuwei23@gmail.com> - 2011-12-22 18:10 -0800
Re: what does 'a=b=c=[]' do Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-22 19:59 -0700
Re: what does 'a=b=c=[]' do alex23 <wuwei23@gmail.com> - 2011-12-22 19:40 -0800
Re: what does 'a=b=c=[]' do Chris Angelico <rosuav@gmail.com> - 2011-12-23 15:25 +1100
Re: what does 'a=b=c=[]' do Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-22 22:22 -0700
Re: what does 'a=b=c=[]' do alex23 <wuwei23@gmail.com> - 2011-12-22 22:00 -0800
Re: what does 'a=b=c=[]' do rusi <rustompmody@gmail.com> - 2011-12-23 00:38 -0800
Re: what does 'a=b=c=[]' do Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-23 09:39 +0000
Re: what does 'a=b=c=[]' do rusi <rustompmody@gmail.com> - 2011-12-23 02:22 -0800
Re: what does 'a=b=c=[]' do Robert Kern <robert.kern@gmail.com> - 2011-12-23 13:10 +0000
Re: what does 'a=b=c=[]' do rusi <rustompmody@gmail.com> - 2011-12-23 05:23 -0800
Re: what does 'a=b=c=[]' do Robert Kern <robert.kern@gmail.com> - 2011-12-23 13:53 +0000
Re: what does 'a=b=c=[]' do rusi <rustompmody@gmail.com> - 2011-12-23 06:57 -0800
Re: what does 'a=b=c=[]' do Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-23 15:33 +0000
Re: what does 'a=b=c=[]' do rusi <rustompmody@gmail.com> - 2011-12-23 07:59 -0800
Re: what does 'a=b=c=[]' do Ethan Furman <ethan@stoneleaf.us> - 2011-12-23 00:49 -0800
Re: what does 'a=b=c=[]' do Chris Angelico <rosuav@gmail.com> - 2011-12-23 20:59 +1100
Re: what does 'a=b=c=[]' do rusi <rustompmody@gmail.com> - 2011-12-23 02:31 -0800
Timeout when calling COM objects on Windows Wong Wah Meng-R32813 <r32813@freescale.com> - 2011-12-23 11:20 +0000
Re: what does 'a=b=c=[]' do Michael Torrie <torriem@gmail.com> - 2011-12-23 10:23 -0700
Re: what does 'a=b=c=[]' do Neil Cerutti <neilc@norwich.edu> - 2011-12-23 13:10 +0000
Re: what does 'a=b=c=[]' do Neil Cerutti <neilc@norwich.edu> - 2011-12-23 13:13 +0000
Early and late binding [was Re: what does 'a=b=c=[]' do] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-23 15:49 +0000
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] Chris Angelico <rosuav@gmail.com> - 2011-12-24 02:55 +1100
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-23 22:32 +0000
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] Chris Angelico <rosuav@gmail.com> - 2011-12-24 09:50 +1100
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-24 08:11 +0000
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] Roy Smith <roy@panix.com> - 2011-12-23 11:15 -0500
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] alex23 <wuwei23@gmail.com> - 2011-12-24 05:43 -0800
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] Mel Wilson <mwilson@the-wire.com> - 2011-12-23 11:27 -0500
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] alex23 <wuwei23@gmail.com> - 2011-12-24 05:52 -0800
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] Neil Cerutti <neilc@norwich.edu> - 2011-12-23 17:03 +0000
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-24 08:25 +0000
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] alex23 <wuwei23@gmail.com> - 2011-12-24 06:08 -0800
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] Devin Jeanpierre <jeanpierreda@gmail.com> - 2011-12-24 18:25 -0500
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] alex23 <wuwei23@gmail.com> - 2011-12-24 16:10 -0800
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] Devin Jeanpierre <jeanpierreda@gmail.com> - 2011-12-24 19:32 -0500
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] rusi <rustompmody@gmail.com> - 2011-12-24 19:22 -0800
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] Lie Ryan <lie.1296@gmail.com> - 2011-12-25 15:12 +1100
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] Devin Jeanpierre <jeanpierreda@gmail.com> - 2011-12-23 19:24 -0500
Re: Early and late binding [was Re: what does 'a=b=c=[]' do] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-24 08:26 +0000
Re: what does 'a=b=c=[]' do Ethan Furman <ethan@stoneleaf.us> - 2011-12-23 00:38 -0800
Re: what does 'a=b=c=[]' do Ethan Furman <ethan@stoneleaf.us> - 2011-12-22 05:20 -0800
Re: what does 'a=b=c=[]' do Eric <einazaki668@yahoo.com> - 2011-12-22 19:46 -0800
Re: what does 'a=b=c=[]' do Lie Ryan <lie.1296@gmail.com> - 2011-12-24 21:30 +1100
Re: what does 'a=b=c=[]' do Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-12-24 19:49 +0100
csiph-web