Message55030
> 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.
|
|
Date |
User |
Action |
Args |
2007-08-23 16:12:36 | admin | link | issue1673203 messages |
2007-08-23 16:12:36 | admin | create | |
|