View Single Post
  #9   Spotlight this post!  
Unread 04-02-2013, 18:02
tux tux is offline
Registered User
AKA: Lee Harr
FRC #3842 (Shock-a-Bots)
Team Role: Mentor
 
Join Date: Apr 2005
Rookie Year: 2005
Location: Rochester, NY
Posts: 91
tux is an unknown quantity at this point
Re: Announcing a new RobotPy tool: fake-wpilib!

Quote:
Originally Posted by virtuald View Post
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
Reply With Quote