Skip to content

Instantly share code, notes, and snippets.

@lexruee
Last active August 29, 2015 14:20
Show Gist options
  • Save lexruee/446148734b12d3d5512b to your computer and use it in GitHub Desktop.
Save lexruee/446148734b12d3d5512b to your computer and use it in GitHub Desktop.
Counter example in Python
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
Copy link

Oh god, my eyes!

@lexruee
Copy link
Author

lexruee commented Apr 29, 2015

@zombiecalypse
You right the other functions are ugly. I just added this nice my_clock() function :-)

@zombiecalypse
Copy link

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

@lexruee
Copy link
Author

lexruee commented Apr 29, 2015

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