On Thu, Feb 18, 2021 at 1:00 PM Christopher Barker <python...@gmail.com> wrote: > > > On Wed, Feb 17, 2021 at 9:11 PM Sven R. Kunze <srku...@mail.de> wrote: >> >> >> Still think that "object()" should be writable since this seems like an >> >> arbitrary restriction > > ... >> >> But I guess there's been discussion around this already. > > > > ... but changing object would be problematic. > > Well, yes, due to backward compatibility -- though how much code is counting > on not being able to add attributes to an instance of object? > > I think someone on this thread (sorry can't find it now) said something like: > > if you could add attributes to object(), then you'd be able to add attributes > to subclasses of object. -- but you can already: > > Isn't every class a subclass of object? > > class C: > pass
You can add attributes to THIS subclass of object. But you can't add attributes to EVERY subclass of object. For instance: x = 1 x.something = 123 If object() could have attributes added, then 1 would have to be able to have attributes added too. And that would cause all manner of problems. > It seems the difference is that both a new class and class instances get a > __dict__. But given that all classes derive from object, and object is of > type type, and classes are of type type -- I still have no idea why we can't > add things to an instance of object. > When you create a class as you did above, it gets a __dict__. But if you set __slots__, the only valid attributes are those listed in the slots. These are inherited: >>> class X: __slots__ = 'a', 'b' ... >>> class Y(X): __slots__ = 'q', 'w' ... >>> Y.__slots__ ('q', 'w') >>> Y().a = 1 >>> Y().q = 1 >>> Y().z = 1 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'Y' object has no attribute 'z' But if object() could get arbitrary attributes, then __slots__ wouldn't work. ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/2PQGNNDNE7WDVQWFK7HCFXX26M4ZROAV/ Code of Conduct: http://python.org/psf/codeofconduct/