Given an arbitrary class X as input, is it possible to find out if instances of X will have a __dict__?
I tried hasattr(X, '__dict__'), but that doesn't work because it checks whether the class object has a __dict__:
>>> hasattr(int, '__dict__')
True
>>> vars(5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: vars() argument must have __dict__ attribute
The absence of __slots__ also isn't a guarantee that there's a __dict__:
>>> hasattr(int, '__slots__')
False
>>> vars(5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: vars() argument must have __dict__ attribute
I also considered creating an instance of X with object.__new__(X) (to bypass X.__new__ and X.__init__, which could potentially have undesired side effects), but this fails for built-in types:
>>> hasattr(object.__new__(int), '__dict__')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object.__new__(int) is not safe, use int.__new__()
Is it possible to do this without calling any unknown/untrusted code (like the constructor of X)?