Is it possible to create a python object which does not have either a __slots__
a __dict__
?
Asked
Active
Viewed 37 times
1

Toothpick Anemone
- 4,290
- 2
- 20
- 42
-
Do you want to restrict access to the class' members? If so, and if you're stuck with python, then you're SOL. – cs95 Aug 13 '18 at 03:04
1 Answers
1
As pointed out in this SO python classes defined in C have no __dict__
attribute. So yeah, it is possible and this is how you get built-in classes that don't have it, such as str
.
Other than that... if you look at the data model the __dict__
is used to hold the namespace for user defined modules, classes and functions. So you'll get that attribute even when you define a new class that inherits from a built in class which doesn't have it. (such as str)
>>> class ob(str):
... pass
...
>>> o = ob()
>>> o.__dict__
{}

PabTorre
- 2,878
- 21
- 30