Where do nested functions live?
Rob Williscroft
rtw at freenet.co.uk
Mon Oct 30 05:21:59 EST 2006
Frederic Rentsch wrote in news:mailman.1428.1162113628.11739.python-
list at python.org in comp.lang.python:
> def increment_time (interval_ms):
> outer weeks, days, hours, minutes, seconds, mseconds # 'outer'
> akin to 'global'
> (...)
> mseconds = new_ms - s * 1000 # Assignee remains outer
> m, seconds = divmod (s, 60)
> h, minutes = divmod (m, 60)
> d, hours = divmod (h, 24)
> weeks, days = divmod (d, 7) # No return necessary
>
> The call would now be:
>
> increment_time (msec) # No reassignment necessary
>
>
> Hope this makes sense
Yes it does, but I prefer explicit in this case:
def whatever( new_ms ):
class namespace( object ):
pass
scope = namespace()
def inner():
scope.mseconds = new_ms - s * 1000
m, scope.seconds = divmod (s, 60)
h, scope.minutes = divmod (m, 60)
d, scope.hours = divmod (h, 24)
scope.weeks, scope.days = divmod (d, 7)
The only thing I find anoying is that I can't write:
scope = object()
Additionally if appropriate I can refactor further:
def whatever( new_ms ):
class namespace( object ):
def inner( scope ):
scope.mseconds = new_ms - s * 1000
m, scope.seconds = divmod (s, 60)
h, scope.minutes = divmod (m, 60)
d, scope.hours = divmod (h, 24)
scope.weeks, scope.days = divmod (d, 7)
scope = namespace()
scope.inner()
In short I think an "outer" keyword (or whatever it gets called)
will just add another way of doing something I can already do,
and potentially makes further refactoring harder.
Thats -2 import-this points already.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
More information about the Python-list
mailing list