[Tutor] Classes Turn Strings Into Tuples for some reason

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Oct 27 07:08:36 CEST 2004



On Wed, 27 Oct 2004, Liam Clarke wrote:

> Hi John
>
> This part here...
>  self.name = name,
>
> The comma tells Python that self.name is a tuple, similar to
> x=(4,5,6,7,) would.


Yes, exactly.  Python's syntax is a bit permissive when it comes to using
tuples.  For example, both:

    (x, y, z) = ('henry', 'richard', 'john')

and:

    x, y, z = 'henny', 'richard', 'john'

do the same thing: the two statements both do a "tuple assignment". And in
the second case, Python can figure out that we are doing tuple assignment,
even without the parentheses.


As a variation of this, we can also do:

    kings = 'thengel', 'theoden', 'eomer'

or:

    havens = 'lothlorien', 'rivendell'

or even:

    one = 'ring',

This is one of the few places where I think this particular permissive
syntax --- optional parentheses for tuples --- might not have been such a
good idea: I think that the mandatory parentheses would improve the
readability of the language.  But since it's in the language, we'd better
be aware of it.



More information about the Tutor mailing list