Ah Python, you have spoiled me for all other languages

Ethan Furman ethan at stoneleaf.us
Fri May 22 17:15:22 EDT 2015


On 05/22/2015 01:34 PM, Marko Rauhamaa wrote:
> Ian Kelly <ian.g.kelly at gmail.com>:
>
>> An "object" in Javascript is basically just a collection of
>> properties. For example:
>>
>> js> typeof([1, 2, 3])
>> "object"
>> js> typeof({a: 1, b: 2, c: 3})
>> "object"
>>
>> Here's what happens when you try to access a property on null:
>>
>> js> null.foo
>> typein:18:0 TypeError: null has no properties
>
> That's not all that different from Python, where object() returns a
> fresh instance at each invocation. However:
>
>     >>> object().x = 3
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in <module>
>     AttributeError: 'object' object has no attribute 'x'
>
> Why are object instances immutable in Python?

# psuedo-code

class object:
     __slots__ = ()

class bad_object:
     "__slots__ not specified, so automatically gets a __dict__"

class tuple(...) <- object or bad_object?  hint: the clue is in the name ;)
     __slots__ = ()

If tuple is based on bad_object, it will get a __dict__ and be very-mutable.

In order to have slightly-mutable or immutable objects, the base class has to not have a __dict__, so object does not.

Mind you, I don't know if that's the reason why it was decided to be like that, or just a nice consequence of the choice to do it that way.

--
~Ethan~



More information about the Python-list mailing list