[Tutor] Fwd: What's in a name?
Mark Lawrence
breamoreboy at yahoo.co.uk
Fri Jan 3 08:23:21 CET 2014
On 03/01/2014 06:55, Keith Winston wrote:
> Mark wrote: You enjoy making life difficult for yourself :) You've
> assigned strings to the name func, just assign the functions themselves?
> Like.
>
>
> for func in max, min:
> print(func.__name__, func(range(5)))
>
> Output.
>
> max 4
> min 0
>
>
> I wouldn't say I enjoy making life difficult for myself, but it is one
> of my strengths ;)
>
> That would work, I think, for the function example I gave you, but the
> example I gave in response to Danny used the same trick on lists: that
> is, I want to iterate through a bunch of lists, subsequently printing
> both list members (via indexing, for example) and the name of the list
> I'm on. I might even want to do the same of a dict, or some random data
> structure. Unless it's just really a bad idea to code like this. But
> certainly when the .__name__ attribute is available, that makes more
> sense. I'll change that part.
>
lista = list(range(5))
listb = list(reversed(range(5)))
for alist in lista, listb:
print(alist.__class__.__name__, alist)
list [0, 1, 2, 3, 4]
list [4, 3, 2, 1, 0]
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Tutor
mailing list