[New-bugs-announce] [issue9702] Python violates most users expectations via the modification differences of immutable and mutable objects in methods

david report at bugs.python.org
Fri Aug 27 15:53:27 CEST 2010


New submission from david <db.pub.mail at gmail.com>:

Python violates most users expectations via the modification differences of immutable and mutable objects in methods.

def foo(bar):
    bar = bar + bar

def listy(bar):
    bar =  [1]

def dicty(bar):
    bar['1'] = '1'

if __name__ == "__main__":
    bar = 1
    foo(bar)
    print bar
    baz = []
    print baz
    listy(baz)
    print baz
    dict_d = {}
    print dict_d
    dicty(dict_d)
    print dict_d

this will output
1
[]
[]
{}
{'1': '1'}


So sure this is 'expected'(pass by reference vs new object - for immutable objects) but it sure isn't obvious.
I feel this is a bug in python core.
I think that the behaviour should be the same for *all* objects.
If it is pass by reference, *and* the item has to be able to be updated(I feel this breaks most people's expectations...) then the result of a modification to an object that is immutable should be that the pointer to the original now points to the resulting string. 

Personally I do not want to be able to modify the dictionary as I did above like I did.

----------
messages: 115074
nosy: db
priority: normal
severity: normal
status: open
title: Python violates most users expectations via the modification differences of immutable and mutable objects in methods

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9702>
_______________________________________


More information about the New-bugs-announce mailing list