Stackoverflow question: Is there a built-in identity function in Python?

Chris Angelico rosuav at gmail.com
Thu Dec 7 16:34:50 EST 2017


On Fri, Dec 8, 2017 at 8:30 AM, Lele Gaifax <lele at metapensiero.it> wrote:
> Chris Angelico <rosuav at gmail.com> writes:
>
>> On Fri, Dec 8, 2017 at 6:29 AM, Lele Gaifax <lele at metapensiero.it> wrote:
>>> Chris Angelico <rosuav at gmail.com> writes:
>>>
>>>> On Fri, Dec 8, 2017 at 5:53 AM, Peter Otten <__peter__ at web.de> wrote:
>>>>>
>>>>> Hm, what does -- and what should --
>>>>>
>>>>> identity(('spam', 'eggs', 7))
>>>>>
>>>>> produce?
>>>>
>>>> The same thing. And so should identity((('spam', 'eggs', 7))) and
>>>> identity(((('spam', 'eggs', 7)))) and identity((((('spam', 'eggs',
>>>> 7))))).
>>>>
>>>> For consistency, identity 'spam', 'eggs', 7 should work too.
>>>
>>> So you think that
>>>
>>>   identity('spam', 'eggs', 7) \
>>>     == identity(('spam', 'eggs', 7)) \
>>>     == identity((('spam', 'eggs', 7),)) \
>>>     == identity(((('spam', 'eggs', 7),),))
>>>
>>> should yield True?
>>
>> No, because you're adding commas. Commas are, like, really important, yo?
>
> I probably misunderstood your "The same thing" answer to Peter's question,
> but IMHO `identity(('spam','eggs',7))` should not return the same as
> `identity('spam','eggs',7)` as I got from your answer, should it?
>

That's exactly the point under discussion. According to Ethan's posted
implementation, multiple arguments get collected into a tuple. That
means that passing three arguments results in a tuple, but passing a
single argument (that happens to be a tuple of three things) returns
that argument unchanged. So, yes, the two would return the same value.

ChrisA



More information about the Python-list mailing list