Quote:
Originally Posted by virtuald
I am curious why you're not using a class-based approach, however.
|
None of our students have any programming experience. I think it is simpler to start with a function-based system.
I have introduced a decorator that sets up static variables (sort of like in C) which helps to alleviate the need for global variables that the functional style sometimes requires.
Code:
def static(**kw):
'''
Used to create a decorator function that will add an
attribute to a function and initialize it.
>>> @static(foo=5)
... def bar():
... print(bar.foo)
... bar.foo += 1
...
>>> bar()
5
>>> bar()
6
'''
def decorator(f):
f.__dict__.update(kw)
return f
return decorator