News for hackers. Stuff that matters.
Re:Anti-Patterns in Python Programming by Dylan16807 on Wednesday July 09, 2014 @03:25PM (#8011729)
> if an object is immutable, the behavior of Python matches what the naive developer expects

If the object was immutable then append wouldn't work. That's hardly matching expectations.

Re:Anti-Patterns in Python Programming by pyre on Wednesday July 09, 2014 @04:09PM (#8011960)
Most immutable objects don't have methods that would mutate the value, but fail because the object is immutable...

I guess the clarification to what I was saying is that, in the simple case (integers, strings, None) the objects are immutable. It's only getting into cases where the value of the object itself is mutable, that you run into issues. If all objects (or all objects 'allowed' as default values) were immutable, then this behavior would not trigger.

So saying that mutability has nothing to do with it isn't entirely true. It's the immutability of the types of values used in most simple cases that hides this issue from developers until they run into a more complex case.

Re:Anti-Patterns in Python Programming by SEJeff on Wednesday July 09, 2014 @06:52PM (#8012654)
Read my post that has the "correct answers" which show you how to do it. The key is setting the default to None and then doing something like:

if val is None: val = []

or the more idiomatic python way:

    val = val or []
Re:Anti-Patterns in Python Programming by CognitiveLens on Thursday July 10, 2014 @12:10PM (#8015439)
I believe the former is more idiomatic, but I don't have a reference.

You want to explicitly check against `None` so that you're not overwriting all falsey values of `val` - even though you should generally try to enforce argument types, your second example would cause unexpected behavior in some cases, particularly those that have non-falsey 'default' assignments

Re:Anti-Patterns in Python Programming by Dylan16807 on Wednesday July 09, 2014 @09:56PM (#8013191)
I'm well aware of that way to do it, but it doesn't excuse a different way being unintuitive.
Re:Anti-Patterns in Python Programming by Paradigma11 on Thursday July 10, 2014 @01:50AM (#8013819)
Why? You would just get a new list back.
Re:Anti-Patterns in Python Programming by msellout on Thursday July 10, 2014 @02:46AM (#8013948)
I can see why you might think so, but remember the Zen of Python is to have only one obvious way to do something.

    >>> [1,2,3] + [4,5]
    [1, 2, 3, 4, 5]
Thus appending should do something different than addition.

    >>> x = [1,2,3]
    >>> x.append([4,5])
    >>> x
    [1, 2, 3, [4, 5]]