Variables versus name bindings [Re: A certainl part of an if() structure never gets executed.]
Roel Schroeven
roel at roelschroeven.net
Thu Jun 20 13:19:56 EDT 2013
Νίκος 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 at roelschroeven.net
More information about the Python-list
mailing list