What is a function parameter =[] for?

Alan Bawden alan at csail.mit.edu
Wed Nov 25 23:27:43 EST 2015


Chris Angelico <rosuav at gmail.com> writes:

> On Thu, Nov 26, 2015 at 1:08 PM, Alan Bawden <alan at csail.mit.edu> wrote:
>> (Note that nothing in the documentation I can find actually _guarantees_
>> that a Python implementation will only have one unique empty tuple, but
>> I wouldn't be suprised if the following is nonetheless true in all
>> current implementations:
>>
>>    >>> tuple([]) is tuple([])
>>    True
>>
>> )
>
> Jython 2.5.3 (, Oct 8 2014, 03:39:09)
> [OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.7.0_85
> Type "help", "copyright", "credits" or "license" for more information.
>>>> tuple([]) is tuple([])
> False
>
> Python 2.7.8 (2.4.0+dfsg-3, Dec 20 2014, 13:30:46)
> [PyPy 2.4.0 with GCC 4.9.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>>> tuple([]) is tuple([])
> False
>
> ChrisA

I said I wouldn't be suprised if it was always true, but that doesn't
imply that I need to be suprised if it is sometimes false!

Having said that, I _am_ curious whether that remains False for various
other variant expressions.  "tuple([])" is actually a rather perverse
way to obtain an empty tuple.  How about plain "() is ()"?  While
"tuple([]) is tuple([])" is False is some implementations, it is still
possible that "() is ()" is True in those same implementations,
depending on how that compiles.

An even more perverse way to obtain an empty tuple is "tuple(x for x in ())"
and in fact in CPython:

   >>> tuple(x for x in ()) is tuple(x for x in ())
   False

If I had thought to try that earlier, I wouldn't have bothered making
the parenthetical remark to are responding to -- because none of this
has any relevance to my original point about "BUILD_TUPLE 0".

-- 
Alan Bawden



More information about the Python-list mailing list