[Tutor] the sense of brackets

spir denis.spir at free.fr
Sat Nov 22 22:58:48 CET 2008


W W a écrit :
 > On Sat, Nov 22, 2008 at 9:42 AM, spir <denis.spir at free.fr> wrote:
 >
 >> I have long thought "[]" /simply/ is a list constructor syntax.
 >> What do you think of the following?
 >>
 >> t = "aze"
 >> print t, list(t), [t]
 >> print list(list(t)), list([t]), [list(t)], [[t]]
 >> ==>
 >> aze ['a', 'z', 'e'] ['aze']
 >> ['a', 'z', 'e'] ['aze'] [['a', 'z', 'e']] [['aze']]
 >
 > Consider the following:
 > In [1]: list("Hello")
 > Out [1]: ['H', 'e', 'l', 'l', 'e', 'o']
 > and the list docstring:
 > list() -> new list
 > list(sequence) -> new list initialized from sequence's items
 > so list(list(t)) makes perfect sense: list(t) is ['a', 'z' ,'e'] and
 > list(list(t)) simply creates a new list initialized from that list's items
 > HTH,
 > Wayne

Yep! What surprises me is the behaviour of [] instead. I can understand that
list(t) != [t]
but
[list(t)], [[t]] --> [['a', 'z', 'e']] [['aze']]
is a bit strange to me.

denis





More information about the Tutor mailing list