Using object as a class
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Mon Mar 26 09:22:07 EDT 2018
On Mon, 26 Mar 2018 07:31:06 -0500, D'Arcy Cain wrote:
> Is this behaviour (object not quite like a class) documented anywhere?
It's exactly like a class. It's an immutable class. You are making
assumptions about what classes must be able to do.
> Does anyone know the rationale for this if any?
object is intended as the base class for all others. As such, it is
absolutely dead simple, with no state and very few methods other than
those required by everything. The usual way we use object instances is as
identity-objects: objects with (almost) no behaviour other than their
identity:
SENTINEL = object()
# later
if obj is SENTINEL: ...
A bit like None.
The usual way to get what you want is a simple subclass of object, but
even more convenient:
py> from types import SimpleNamespace
py> x = SimpleNamespace(x=1, y=2, z=3)
py> x
namespace(x=1, y=2, z=3)
--
Steve
More information about the Python-list
mailing list