Last active
August 29, 2015 14:20
-
-
Save lexruee/446148734b12d3d5512b to your computer and use it in GitHub Desktop.
Counter example in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def a(x=[0]): | |
x.append(x[-1] + 1) | |
return x[-1] | |
print a() | |
print a() | |
print a() | |
def b(x=[0]): | |
x[0] += 1 | |
return x[0] | |
print b() | |
print b() | |
print b() | |
def my_clock(x=[0]): | |
x[0] = (x[0] + 1) % 24 | |
return x[0] | |
for _ in range(0,24 * 2): | |
print "time: %s" % my_clock() | |
def ring_counter(x=[0], c=24): | |
x[0] = (x[0] + 1) % c | |
return x[0] | |
for _ in range(0, 24 * 2): | |
print ring_counter(c=12) | |
@zombiecalypse
You right the other functions are ugly. I just added this nice my_clock() function :-)
Why not
def clock1():
i = 0
while True:
yield i%24
a = clock1()
print next(a)
print next(a)
Or maybe
def clock2():
i = 0
def tock():
i+=1
return I
return tock
Of course a generator and anonymous function does the same job :-).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh god, my eyes!