about sort and dictionary

bonono at gmail.com bonono at gmail.com
Sun Nov 20 14:38:00 CET 2005


Shi Mu wrote:
> Got confused by the following code:
> >>> a
> [6, 3, 1]
> >>> b
> [4, 3, 1]
> >>> c
> {1: [[6, 3, 1], [4, 3, 1]], 2: [[6, 3, 1]]}
> >>> c[2].append(b.sort())
> >>> c
> {1: [[6, 3, 1], [1, 3, 4]], 2: [[6, 3, 1], None]}
> #why c can not append the sorted b??
> >>> b.sort()
> >>> b
> [1, 3, 4]
most built-in function/method don't return the "object" but None. This
I believe is the language creator's preference for everything being
explicit. You better do it like this :

b.sort()
c[2].append(b)

Of course, this make things like this not possible :

obj.method_a().method_b().method_c()

But the language(and the community) in general discourage you to write
code like this ;-)




More information about the Python-list mailing list