[Tutor] sorting / outputting varibales

Evert Rol evert.rol at gmail.com
Thu Nov 1 19:52:35 CET 2007


> If i input the following in python:
>    var1 = 7
>    var2 = 9
>    var3 = 5
>    args = [var1, var2, var3]
>    args.sort()
>
> then if if type:
>
>    args
>
> the output is
>
>    [5, 7, 9]
>
> but i want the output to be
>
>    [var3, var1, var2]
>
> is there a function that will output the variable
> names in the order they have been sorted instead of
> the variable contents themselves?


Hm, I don't know directly how to get the name of a variable. I'd  
hoped there was a private method (var3.__<something>__), but looks  
like there isn't it.

Perhaps you can do something with a dictionary, eval and/or map? Not  
sure how that would work out, you'll have to play around a bit to get  
it working.

But I'm not really sure why you'd want to pass the variable names to  
a function; in the end, you're going to use the actual values anyway,  
not their names, right?




More information about the Tutor mailing list