On Sat, Jan 2, 2016 at 10:42 PM, Nick Coghlan <ncoghlan@gmail.com> wrote:
On 3 January 2016 at 13:48, Guido van Rossum <guido@python.org> wrote:
> Whoops, Nick already did the micro-benchmarks, and showed that creating a
> function object is faster than instantiating a class. He also measured the
> size, but I think he forgot that sys.getsizeof() doesn't report the size
> (recursively) of contained objects -- a class instance references a dict
> which is another 288 bytes (though if you care you can get rid of this by
> using __slots__).

You're right I forgot to account for that (54 bytes without __slots__
did seem surprisingly small!), but functions also always allocate
f.__annotations__ at the moment.

Always allocating f.__annotations__ actually puzzled me a bit - did we
do that for a specific reason, or did we just not think of setting it
to None when it's unused to save space the way we do for other
function attributes? (__closure__, __defaults__, etc)

Where do you see that happening? The code in funcobject.c seems to indicate that it's created on demand. (And that's how I remember it always being.)

--Guido van Rossum (python.org/~guido)