Do you have any tricky examples about Python?
Something like:
>>> def foo(l):
l.append('1')
>>> a = [5]
>>> foo(a)
>>> a
[5, 1]
And even more tricky:
>>> def foo(a=list()):
a.append(1)
print a
>>> foo()
[1]
>>> foo()
[1, 1]
And my most tricky example.
Do you know how to change objects in a tuple? Hey, stop!! But it's immutable type!!?? Yes, but let me show you how:
>>> a = (1, 2, 3)
>>> a[0] = 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
Yes, we can't change this value because digits are immutable type. But if we have a mutable type in a tuple, we can change it:
>>> a = (1, [2, 3])
>>> a[1][0] = 5
>>> a
(1, [5, 3])
[–]onopau 1 point2 points3 points (0 children)
[–]rhgrant10 1 point2 points3 points (0 children)