
Output from both 3.4.1 and 3.5.0:
def foo(): pass>>> foo.__annotations__{} Probably an oversight. I'm also not a C expert, but func_get_annotations (line 396 and onwards in funcobject.c) explicitely returns a new, empty dict if the function doesn't have any annotations (unlike all the other slots, like __defaults__ or __kwdefaults__, which merely return None if they're not present). From: guido@python.org Date: Mon, 4 Jan 2016 16:26:53 -0800 To: ncoghlan@gmail.com Subject: Re: [Python-ideas] Bad programming style in decorators? CC: python-ideas@python.org; surya.subbarao1@gmail.com
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) _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/