Where do nested functions live?

Frederic Rentsch anthra.norell at vtxmail.ch
Wed Nov 1 12:48:44 EST 2006


Rob Williscroft wrote:
> Frederic Rentsch wrote in news:mailman.1556.1162316571.11739.python-
> list at python.org in comp.lang.python:
>
>   
>> Rob Williscroft wrote:
>>     
>>> Frederic Rentsch wrote in news:mailman.1536.1162292996.11739.python-
>>>       
>>>> Rob Williscroft wrote:
>>>>         
>>>>> Frederic Rentsch wrote in news:mailman.1428.1162113628.11739.python-
>>>>>           
>
> [snip]
>
>   
>>>>>       
>>>>>           
>>>> Here I'm lost. What's the advantage of this? It looks more convoluted.
>>>>     
>>>>         
>>> I'll agree that having to explicitly define a namespace class first 
>>> does add some convolution. 
>>>
>>> But if you refer to having to prefix you "outer" variables with
>>> "scope." then this would be the same as claiming that the explict use
>>> of self is convoluted, which is a valid opinion, so fair enough, but
>>> I can't say that I agree. 
>>>
>>>   
>>>       
>> I didn't mean to call into question. I didn't understand the advantage 
>> of the added complexity of your second example over the first.
>>
>>     
>
> Ok, I missed your point, as for the second example it was an attempt 
> to show that further refactoring from a function with local functions
> that are sharing some state via the scope object, to a class with
> methods that share state via the instance, is a matter of editing
> a few lines.  
>
> This is useful when a function grows too large (for some value of 
> "too large"). As an added bonus you get to use the same thechniques
> with both styles of coding.
>  
> [snip]
>
> Rob.
>   

Rob,

Thanks a lot for your input. I'll have to digest that. Another question 
I had and forgot was this: Since we have a class that goes out of scope 
when the function returns, and we don't need more than one instance, why 
bother to make an instance? Why not use the class object itself?

def whatever( new_ms ):

  class scope ( object ):

  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)     


Frederic









More information about the Python-list mailing list