homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author belopolsky
Recipients
Date 2007年03月22日.19:32:56
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
> I can't believe there is such resistance for something
> as trivial as a function which returns it's first argument.
The resistance is precisely because it is so trivial. As josiahcarlson suggested before, "Not all x line functions should be built into Python." Compare the following two alternatives:
def identity(*args): return args[0]
and
from functools import identity
The first option is only slightly longer than the second, but it is actually much clearer. With the second option, it is not obvious that identity takes an arbitrary number of arguments and if it does, which argument it will return.
The only advantage is that if coded in C, functools.identity may be slightly faster. However, given that 
>>> dis(lambda x: x)
 1 0 LOAD_FAST 0 (x)
 3 RETURN_VALUE
I am not sure a C coded identity can significantly beat it. The real savings would come if python compiler could optimize away calls to identity altogether, but nobody has explained how that could be done or why it would be easier to optimize away identity that lambda x:x.
History
Date User Action Args
2007年08月23日 16:12:36adminlinkissue1673203 messages
2007年08月23日 16:12:36admincreate

AltStyle によって変換されたページ (->オリジナル) /