Encapsulation unpythonic?

Ned Batchelder ned at nedbatchelder.com
Sat Aug 31 08:41:54 EDT 2013


On 8/31/13 7:46 AM, Steven D'Aprano wrote:
> On Fri, 30 Aug 2013 23:07:47 -0700, Fabrice Pombet wrote:
>
>> well, look at that:
>>
>> a=(1,2)
>> a=2+3 ->a is an object and I have changed its type and value from
>> outside.
> Incorrect. You have not changed the type or value of any object. "a" is
> not an object, it is a *name*, and while you can change the object bound
> to the name, the objects remain unchanged.
>
> When you do this:
>
> x = 23
> x = 42
>
> the *object* 23 does not change, only the name binding changes. To do
> otherwise would cause all sorts of surprises:
>
> # THIS DOES NOT HAPPEN IN PYTHON
> # or any other language, as far as I am aware
> x = 23
> y = x  # y now has the value 23
> x = 42  # change the value of the object  ### NOT SO! ###
> print y
> => prints 42
>
> Name binding (assignment) does not change objects. It changes the link
> between a name and the object, but the object remains untouched (unless
> it is unbound, and garbage collected). Assignment is not mutation.
> Assigning to a name does not modify the object that was previously bound.
>

I wrote a piece about names and values that might help clarify these 
points: Facts and Myths about Names and Values in Python: 
http://nedbatchelder.com/text/names.html

--Ned.



More information about the Python-list mailing list