[Tutor] The magic parentheses

Alan Gauld alan.gauld at btinternet.com
Mon Jan 25 09:36:24 CET 2010


"David Hutto" <dwightdhutto at yahoo.com> wrote

>> >>> a = 1
>> >>> b = 2
>> >>> c = 3
>> >>> x = a,b,c
>> >>> print a,b,c
>> 1 2 3
>> >>> print x
>> (1, 2, 3)

> So 'print a,b,c' says display the values of a,b,c in the sequence of 
> a,b,c given.
> 'print x' says print the value of x, x grabs a's value, b's value, and 
> c's value,
> and displays the values for print.

Not quite.
x is a tuple - a collection of values - it does not grab those values, it 
is given them
Once given them it keeps them. If we subsequently change 'a' that change 
will
not be reflected in x! (Note it gets more complex if 'a' is a mutable 
type.)

>>> a = 1
>>> b = 2
>>> c = 3
>>> x = a,b,c
>>> print a,b,c
1 2 3
>>> print x
(1, 2, 3)
>>> a = 66
>>> x
(1, 2, 3)

So no change in x even though a has changed.

print always just displays the string representation of the values passed 
to it.
So when passed a,b,c it prints the string version of a, the string version
of b and the string version of c. When passed x it prints the string 
version
of x which is a tuple. tuples in Python are represented as a sequence of
values inside parentheses. But x does not go and fetch the latest values
of a,b and c, it uses the values provided when x was created.

> So the sequence becomes a tuple because it's a value of values,

x is a tuple because that's what we created when we assigned the 3 values.
Any sequence of values outside brackets or quotes is a tuple

>>> t1 = 1,2,3
>>> t2 = 'a','b','c'
>>> t3 = (2,,4,6)
>>> t4 = (t1,t2)

> and it's sum comes back in the form of a tuple,

It was a tuple, it does not create itself dynamically.

> not an actual end variable.

A tuple is an "end variable". It is just as much a valid type as an
integer, a string or a list. It is just another data type.

> In other words the second leap to find the values of a,b,c for value x,

There is no second leap. It stores and uses the values passed to it in
the original assignment.

> Now if I want to turn the first values listed under x into tuples
> (or use the format function that I think was named in another
> reply to return the non-tuple version of the value for x and
> I'm going to try it in a few min) of the other two variables,

I'm not sure what you mean here.
The values in x are the original values of a,b and c.
You can access those values using x[0],x[1],x[2].
Is that what you mean?

> g = f
> a = b,c

This will fail because b and c don;t exist yet.

> b = a,c

this will fail because c does not exist yet

> c = a,g
> x = a,b,c


> This is off the topic I think, so I'll repost under another if necessary,
> but how do I avoid going into the loop of tuples/variables.

There is no loop, it is not a dynamic lookup. The tuple stores the
values at the time of creation. If the values don;t exist yet(as above)
the assignment will fail to create a tuple and you will get an error.

> If a is dependent on knowing what b,c are, and b is dependent
> on knowing what a and c are, and c is dependent on knowing
> what a and b are, how do I prevent from going into a defining
> loop for each.

Python won;t let you create variables using other variables that
have not been defined yet. It is your responsibility as the programmer
to define valriables  before using them.

> This is just basically playing with Python's functions, I was
> just wondering how to approach such a problem where variables
> must come in order but might be dependent on a following variable

It will fail. You must create the variables before use.
We, can do some clever things with functions that can defer
the evaluation of variables until later in the program but thats
an advanced topic that I won't go into here!

> In the above, if I want to know c it requires knowing a,g. But
> in order to know a, a needs to know b,c-so I can't list c
> above a or a above c, because I get the error that the
> value of the other is not known when I try to run the script.

Correct, you must find a way to order them such that the
dependencies are sorted out before assignment.

Take a look at the sections on variables and collections,
especially tuples, in the Raw Materials topic of my tutor for
more information..


HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list