[Python] Why are instances of the `object` class immutable in Python?

Discussão em 'Python' iniciado por Stack, Outubro 24, 2024.

  1. Stack

    Stack Membro Participativo

    >>> a = object()
    >>> a.x = 5
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    AttributeError: 'object' object has no attribute 'x'
    >>> b = lambda:0
    >>> b.x = 5
    >>> b.x
    5


    Why do instances of the object class not have a __dict__, causing it to behave as semantically immutable? What were the reasons for choosing this design?

    Specifically, why:


    instances of types defined in C don't have a __dict__ attribute by default.
    ​

    As noted in this question.

    Continue reading...

Compartilhe esta Página