What is a function parameter =[] for?

Alan Bawden alan at csail.mit.edu
Wed Nov 25 21:08:17 EST 2015


Steven D'Aprano <steve at pearwood.info> writes:
> In fact, it's easy to find cases even now where the compiler is
> insufficiently smart to recognise all the possible optimizations available.
> There's no tuple simpler than the empty tuple, but Python 3.3 at least
> fails to optimize that case:
>
> py> dis(compile("()", '', 'eval'))
>   1           0 BUILD_TUPLE              0
>               3 RETURN_VALUE

Actually, it would be silly to compile that as a LOAD_CONST of an empty
tuple.  "BUILD_TUPLE 0" doesn't allocate storage, it returns the same
empty tuple every time!  In effect "BUILD_TUPLE 0" is just a global
load.  A different disassembler might decide to print "BUILD_TUPLE 0" as
"LOAD_EMPTY_TUPLE".

Using LOAD_CONST would just waste space in the function's vector
of constants.

(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

)

-- 
Alan Bawden



More information about the Python-list mailing list