Bizzare lst length problem

Fredrik Lundh fredrik at pythonware.com
Sun Oct 8 09:38:01 EDT 2006


Ben wrote:

> Ah - I found out why I had cast it to a string. I had not, at that
> point, worked out ho to pass the list by value rather than reference,
> and so was casting to a string as a stopgap measure that I then forgot
> about. Now the problem is fixed after this group told me how to pass a
> list by value (by slicing the entire list)

if you write code that needs to treat a list as a distinct mutable 
value, make sure *your* code makes a copy.  relying on the caller to 
remember to do that in all cases is way too error prone.

in other words, instead of doing

    def function(seq):
        # modify the sequence

    ...

    # must pass in a copy, or things will break in mysterious ways
    function(list(mylist))

do

    def function(seq):
        seq = list(seq) # make a distinct copy
        # modify the sequence

    ...

    function(seq)

</F>




More information about the Python-list mailing list