about types.new_class and module

Jimmy Girardet ijkl at netc.fr
Mon Mar 4 13:02:44 EST 2019


Hello,

I'm looking for an explanation where live classes created by
types.new_class() :

py> import types

py> types.new_class('A')

types.A

py> types.A

AttributeError: module 'types' has no attribute 'A'

py> _.__module__

'types'


The new class comes from `types` module without being inside.

That's annoying to me for my use case :

I'm trying to create dataclasses on the fly using  make_dataclass (which
uses types.new_class). For new created classes, I have a cache to not
recreate twice the same class.

But I want to be sure not to override an existing class somewhere in the
namespace which is already 'types.MyNewclass'. but how to check it if
it's not in types ?

To be clear :

make_dataclass('Bla', {}) should raise an error if something named
'types.Bla' already exists.

I hope I'm clear enough.

Jimmy








More information about the Python-list mailing list