Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #25922
| Path | csiph.com!usenet.pasdenom.info!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'example:': 0.03; 'kind,': 0.05; 'modify': 0.05; 'assign': 0.07; 'subject:code': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'references.': 0.09; 'referencing': 0.09; 'unpacking': 0.09; 'stored': 0.10; 'def': 0.10; '12:50': 0.16; '24,': 0.16; '[none,': 0.16; 'bind': 0.16; 'expression,': 0.16; 'iterable': 0.16; 'none]': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'silly': 0.16; 'substitute': 0.16; 'variable.': 0.16; 'wrote:': 0.17; 'instance': 0.17; "shouldn't": 0.17; 'variable': 0.20; 'bit': 0.21; 'references': 0.23; 'this:': 0.23; 'pass': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'rules': 0.27; "doesn't": 0.28; 'header:X-Complaints- To:1': 0.28; 'chris': 0.28; 'container': 0.29; 'objects': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'e.g.': 0.30; 'function': 0.30; 'print': 0.32; 'to:addr:python-list': 0.33; 'code:': 0.33; 'version': 0.34; 'done': 0.34; 'clear': 0.35; 'same.': 0.35; 'subject:?': 0.35; 'received:org': 0.36; 'totally': 0.36; 'subject:with': 0.36; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'list,': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; "you've": 0.61; 'love': 0.63; 'here': 0.65; 'taking': 0.65; 'contents.': 0.65; 'jul': 0.65; 'article': 0.78; 'gain': 0.79; 'subject:this': 0.84; 'russell': 0.84; 'self.value': 0.84; 'magical': 0.93 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | "Russell E. Owen" <rowen@uw.edu> |
| Subject | Re: What's wrong with this code? |
| Date | Mon, 23 Jul 2012 14:00:37 -0700 |
| Organization | University of Washington |
| References | <CAMugt-pRmdy5M_18rbGC13-6_aqVOCUHYX4-C-XaPWKh+dYRtg@mail.gmail.com> <CAPTjJmqrhztsUkRSYb56=TX=hDomVo8mePcSY0yTjAUpTcmJtA@mail.gmail.com> |
| X-Gmane-NNTP-Posting-Host | p172-28-191-0.nat.washington.edu |
| User-Agent | MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) |
| 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.2499.1343077256.4697.python-list@python.org> (permalink) |
| Lines | 66 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1343077256 news.xs4all.nl 6842 [2001:888:2000:d::a6]:60348 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:25922 |
Show key headers only | View raw
In article
<CAPTjJmqrhztsUkRSYb56=TX=hDomVo8mePcSY0yTjAUpTcmJtA@mail.gmail.com>,
Chris Angelico <rosuav@gmail.com> wrote:
> On Tue, Jul 24, 2012 at 12:50 AM, Stone Li <viewfromoffice@gmail.com> wrote:
> >
> > I'm totally confused by this code:
> >
> > Code:
>
> Boiling it down to just the bit that matters:
>
> c = None
> d = None
> x = [c,d]
> e,f = x
> c = 1
> d = 2
> print e,f
>
> When you assign "e,f = x", you're taking the iterable x and unpacking
> its contents. There's no magical "referenceness" that makes e bind to
> the same thing as c; all that happens is that the objects in x gain
> additional references. When you rebind c and d later, that doesn't
> change x, nor e/f.
>
> What you've done is just this:
>
> x = [None, None]
> e,f = x
> c = 1
> d = 2
> print e,f
>
> It's clear from this version that changing c and d shouldn't have any
> effect on e and f. In Python, any time you use a named variable in an
> expression, you can substitute the object that that name is
> referencing - it's exactly the same. (That's one of the things I love
> about Python. No silly rules about what you can do with a function
> return value - if you have a function that returns a list, you can
> directly subscript or slice it. Yay!)
Good explanation.
Perhaps what the original poster needs is a container of some kind, e.g.
a class with the value as an instance variable. Then you can pass around
references to the container and read or modify the value(s) stored in it
when you need them.
Here is a simple example:
class Container(object):
def __init__(self, value):
self.value = value
c = Container(5)
d = Container(6)
x = [c, d]
e, f = x
c.value = None
d.value = "hello"
print e.value, f.value
None "hello"
-- Russell
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: What's wrong with this code? "Russell E. Owen" <rowen@uw.edu> - 2012-07-23 14:00 -0700
csiph-web