Where do nested functions live?

Fredrik Lundh fredrik at pythonware.com
Sun Oct 29 11:33:36 CET 2006

Frederic Rentsch wrote:

> At some later point I need to increment my units some more and probably 
> will again a number of times. Clearly this has to go into a function.

since Python is an object-based language, clearly you could make your 
counter into a self-contained object instead of writing endless amounts 
of code and wasting CPU cycles by storing what's really a *single* state 
in a whole bunch of separate variables.

in your specific example, you can even use an existing object:

    t = datetime.datetime.now()

    # increment
    t += datetime.timedelta(milliseconds=msec)

    print t.timetuple() # get the contents

if you're doing this so much that it's worth streamlining the timedelta 
addition, you can wrap the datetime instance in a trivial class, and do

    t += 1500 # milliseconds

when you need to increment the counter.

 > This is a little like a shop where the mechanics have to get their
 > tools and work pieces from the manager and hand them back to him when 
 > they're done.

that could of course be because when he was free to use whatever tool he 
wanted, he always used a crowbar, because he hadn't really gotten around 
to read that "tool kit for dummies" book.


More information about the Python-list mailing list