3 ms·
for your function example, the a=[] is not a way of initializing values per se, but is an initialized default value. If you are not passing a list reference in
by zephjc 16y ago
for your function example, the a=[] is not a way of initializing values per se, but is an initialized default value. If you are not passing a list reference in, and not returning anything, your function foo() without an arg does nothing. If you pass in a list reference x e.g. foo(x), x will now be [123]
For your class example, this is really just a matter of remembering that any self members must be explicitly referenced as such: self.a.append(123) instead of a.append(123). I have actually grown to like this, and it helps keep it clear to others viewing your code what a ivar/method belongs to. Ruby uses @, C++ optionally uses 'this' - when I do any C++ i find myself explicitly using 'this' - its that or prefix ivars with e.g. 'm_'.
Reading the Zen of Python is a good way of getting into the mindset for idiomatic Python programming