I noticed a strange behaviour in python, could anyone give me the reason for such behaviour?
If I call a function without attribute then the default value is retains its state. You can see the following example for better understanding
class EvenStream(object):
def __init__(self):
self.current = 0
def get_next(self):
to_return = self.current
self.current += 2
return to_return
class OddStream(object):
def __init__(self):
self.current = 1
def get_next(self):
to_return = self.current
self.current += 2
return to_return
def print_from_stream(n, stream=EvenStream()):
for _ in range(n):
print(stream.get_next())
print_from_stream(2)
print('*****')
print_from_stream(2)
print('*****')
print_from_stream(2, OddStream())
print('*****')
print_from_stream(2, OddStream())
OUTPUT: I was expecting 0,2,0,2 but it gave me 0,2,4,6
0
2
*****
4
6
*****
1
3
*****
1
3