[Tutor] Fwd: What's in a name?

Keith Winston keithwins at gmail.com
Fri Jan 3 07:55:29 CET 2014


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140103/d57e3a78/attachment-0001.html>


More information about the Tutor mailing list