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

Ethan Furman ethan at stoneleaf.us
Thu Dec 7 14:20:18 EST 2017


On 12/07/2017 10:53 AM, Peter Otten wrote:
> Ethan Furman wrote:
>
>> The simple answer is No, and all the answers agree on that point.
>>
>> It does beg the question of what an identity function is, though.
>>
>> My contention is that an identity function is a do-nothing function that
>> simply returns what it was given:
>>
>> --> identity(1)
>> 1
>>
>> --> identity('spam')
>> 'spam'
>>
>> --> identity('spam', 'eggs', 7)
>> ('spam', 'eggs', 7)
>
> Hm, what does -- and what should --
>
> identity(('spam', 'eggs', 7))
>
> produce?

Well, since it's the lowly "," that makes a tuple (not the parentheses), those extra parentheses don't have any affect.

If you were trying to get a 3-item tuple inside a 1-item tuple:

(('spam', 'eggs', 7), )

Then you would need:

--> identity( (('spam', 'eggs', 7), ) )
(('spam', 'eggs', 7),)

Okay, actually sometimes it takes both.  ;)

--
~Ethan~



More information about the Python-list mailing list