["baz"]
>>> foo()
["baz", "baz"]
>>> foo()
["baz", "baz", "baz”]
... if bar is None:# or if not bar:
... bar = []
... bar.append("baz")
... return bar
...
>>> foo()
["baz"]
>>> foo()
["baz"]
>>> foo()
["baz”]
... x = 1
...
>>> class B(A):
... pass
...
>>> class C(A):
... pass
…
1 1 1
>>> print A.x, B.x, C.x
1 2 1
>>> print A.x, B.x, C.x
3 2 3
... l = ["a", "b"]
... int(l[2])
... except ValueError, IndexError:
... pass
... l = ["a", "b"]
... int(l[2])
... except (ValueError, IndexError) as e:
... pass
>>> def foo1():
... lst.append(5) # This works ok...
...
>>> foo1()
>>> lst
[1, 2, 3, 5]
>>> lst = [1, 2, 3]
>>> def foo2():
... lst += [5] # ... but this bombs!
...
>>> foo2()
>>> numbers = [n for n in range(10)]
>>> for i in range(len(numbers)):
... if odd(numbers[i]):
... del numbers[i]
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
IndexError: list index out of range
>>> numbers = [n for n in range(10)]
>>> numbers[:] = [n for n in numbers if not odd(n)]
>>> numbers
[0, 2, 4, 6, 8]
... return [lambda x : i * x for i in range(5)]
>>> for multiplier in create_multipliers():
... print multiplier(2)
... return [lambda x, i=i : i * x for i in range(5)]
...
>>> for multiplier in create_multipliers():
... print multiplier(2)
class Bar(object):
...
def __del__(self):
foo.cleanup(self.myhandle)
import atexit
def cleanup(handle):
foo.cleanup(handle)
class Bar(object):
def __init__(self):
...
atexit.register(cleanup, self.myhandle)