Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python.': 0.02; 'variables': 0.07; 'iterate': 0.09; 'objects,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject: [': 0.09; 'def': 0.12; '*always*': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:versus': 0.16; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'refers': 0.24; 'this:': 0.26; 'second': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'see,': 0.30; "d'aprano": 0.31; 'names.': 0.31; 'node': 0.31; 'pascal': 0.31; 'steven': 0.31; 'class': 0.32; 'another': 0.32; 'skip:_ 10': 0.34; 'could': 0.34; 'received:84': 0.35; 'but': 0.35; 'really': 0.36; 'yield': 0.36; 'example,': 0.37; 'list': 0.37; 'thank': 0.38; 'to:addr:python- list': 0.38; 'fact': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'simple': 0.61; 'first': 0.61; 'refer': 0.63; 'skip:n 10': 0.64; 'linked': 0.65; '8bit%:92': 0.71; 'to,': 0.72; 'dict,': 0.84; 'self.value': 0.84; 'subject:gets': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Roel Schroeven Subject: Re: Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.] Date: Thu, 20 Jun 2013 19:19:56 +0200 References: <2bc90d3b-09c2-4315-9357-ff7f039465e0@googlegroups.com> <51bb454c$0$29997$c3e8da3$5496439d@news.astraweb.com> <51beb20c$0$29872$c3e8da3$5496439d@news.astraweb.com> <51c022d5$0$29872$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: d54c6d802.access.telenet.be User-Agent: Thunderbird 2.0.0.24 (Windows/20100228) In-Reply-To: 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1371748820 news.xs4all.nl 15953 [2001:888:2000:d::a6]:50813 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:48812 Νίκος schreef: > Στις 18/6/2013 12:05 μμ, ο/η Steven D'Aprano έγραψε: >> Names are *always* linked to objects, not to other names. >> >> a = [] >> b = a # Now a and b refer to the same list >> a = {} # Now a refers to a dict, and b refers to the same list as before > > I see, thank you Steven. > > But since this is a fact how do you create complicated data structures > that rely on various variables pointing one to another liek we did in > C++(cannot recall their names) ? You almost never need to do that in Python. But if you really want to, out of curiosity, you can. For example, you could create a simple singly linked list like this: >>> class Node(object): def __init__(self, value): self.value = value self.next = None >>> first = Node(1) >>> second = Node(2) >>> first.next = second You could iterate over it like this: >>> def iterate_linked_list(node): while node: yield node.value node = node.next >>> for v in iterate_linked_list(first): print v -- "People almost invariably arrive at their beliefs not on the basis of proof but on the basis of what they find attractive." -- Pascal Blaise roel@roelschroeven.net