[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

about types.new_class and module


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

py> import types

py> types.new_class('A')


py> types.A

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

py> _.__module__


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.