[Python-ideas] Make functions, methods and descriptor types living in the types module

Victor Stinner victor.stinner at gmail.com
Thu Jan 11 17:41:30 EST 2018


I like the idea of having a fully qualified name that "works" (can be
resolved).

I don't think that repr() should change, right?

Can this change break the backward compatibility somehow?

Victor

Le 11 janv. 2018 21:00, "Serhiy Storchaka" <storchaka at gmail.com> a écrit :

> Currently the classes of functions (implemented in Python and builtin),
> methods, and different type of descriptors, generators, etc have the
> __module__ attribute equal to "builtins"  and the name that can't be used
> for accessing the class.
>
> >>> def f(): pass
> ...
> >>> type(f)
> <class 'function'>
> >>> type(f).__module__
> 'builtins'
> >>> type(f).__name__
> 'function'
> >>> type(f).__qualname__
> 'function'
> >>> import builtins
> >>> builtins.function
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: module 'builtins' has no attribute 'function'
>
> But most of this classes (if not all) are exposed in the types module.
>
> I suggest to rename them. Make the __module__ attribute equal to
> "builtins" and the __name__ and the __qualname__ attributes equal to the
> name used for accessing the class in the types module.
>
> This would allow to pickle references to these types. Currently this isn't
> possible.
>
> >>> pickle.dumps(types.FunctionType)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> _pickle.PicklingError: Can't pickle <class 'function'>: attribute lookup
> function on builtins failed
>
> And this will help to implement the pickle support of dynamic functions
> etc. Currently the third-party library that implements this needs to use a
> special purposed factory function (not compatible with other similar
> libraries) since types.FunctionType isn't pickleable.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180111/0b6b2dd5/attachment.html>


More information about the Python-ideas mailing list