Where do nested functions live?

Steve Holden steve at holdenweb.com
Wed Nov 1 21:01:49 CET 2006


Frederic Rentsch wrote:
> 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)     
> 
That will need to be

   class scope(object): pass

to avoid syntax errors, I suspect. There doesn't seem to be any reason 
why you couldn't use a class instead of an instance. And, of course, 
either might give you problems in the case of a recursive inner function.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list