stuff = stuff or []
because if you pass an empty list, you'll get a new one rather than mutating the one you passed. stuff = stuff if stuff is not None else []
is wordy, but at least it's correct. stuff = stuff or []
because if you pass an empty list, you'll get a new one rather than mutating the one you passed. stuff = stuff if stuff is not None else []
is wordy, but at least it's correct. if stuff is None:
stuff = []If I were being cavalier and had an extra wish to burn, I'd request
x else y
to mean x unless x is None.Dammit, people - in Python, "That's clever" is an insult. Learn the philosophy of a language and you'll be much happier using it.
Especially when that keyword is not necessary and when it breaks standard programmer expectations (if I declare a method with 3 parameters, I should call it with 3, not 2).
foo.bar(baz, sputz)
^1 ^2 ^3
When you write a class method, it has more information available to it than a similar subroutine; thus, another parameter.GvR brings up the deeper reasons for explicit self at http://neopythonic.blogspot.com/2008/10/why-explicit-self-ha... .
Python is doing this totally weird stuff that sits in the middle and that makes no logical sense at all.
The fact that Python forces you to declare the "self" parameter is simply due to the fact that it's old, old, old. Nothing wrong with that, but post rationalizing it by saying it's okay to declare a method with 3 parameters but calling with just 2 is just silly.
It lets you define the name of the this parameter in exactly the same way you pass it.
type Bar(p) =
member x.Foo(bar) = printf "%s %d" bar p
let x = new Bar(10)
x.Foo("Foo bar")
Output: Foo bar 10
And yes, the variables bar and p are type inferred from the %s and %d respectively class Flip(object):
foo=4
def flop(x,num):
x.bar=x.foo+numThe error messages related to `self` in method definitions caused me some confusion when starting out with python. For instance if you define a method with no arguments, calling it results in "TypeError: your_method() takes no arguments (1 given)".
stuff = stuff is not None and stuff or []
That said, the `if stuff is not None:` is the most Pythonic way of doing it.