Where do nested functions live?

Rob Williscroft rtw at freenet.co.uk
Tue Oct 31 13:00:17 CET 2006


Frederic Rentsch wrote in news:mailman.1536.1162292996.11739.python-
list at python.org in comp.lang.python:

> Rob Williscroft wrote:
>> Frederic Rentsch wrote in news:mailman.1428.1162113628.11739.python-
>> list at python.org in comp.lang.python:
>>
>>   

>> 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)        
>>
>>   
> 
> This is interesting. I am not too familiar with this way of using 
> objects. Actually it isn't all that different from a list, because a 
> list is also an object. But this way it's attribute names instead of 
> list indexes which is certainly easier to work with. Very good!
> 

>> 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.
>>
>>   
> 
> 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.

It should also be noted that although I have to declare and create a 
scope object. My method doesn't require the attributes passed back from 
the inner function be pre-declared, should I during coding realise
that I need to return another attribute I just assign it in the inner
function and use it in the outer function. I would count that as less
convoluted, YMMV.
 
> And speaking of convoluted, what about efficiency? There is much talk
> of efficiency on this forum. I (crudely) benchmark your previous 
> example approximately three times slower than a simple inner function 
> taking and returning three parameters. It was actually the aspect of
> increased efficiency that prompted me to play with the idea of 
> allowing direct outer writes.

Did you have optimisation turned on ?

As I see it there should be no reason an optimiser couldn't transform 
my code into the same code we might expect from your "outer keyword"
example, as the scope object's type and attributes are all contained 
within (thus known to) the outer function defenition.

Wether CPython's optimiser does this or not, I don't know.

> 
>> Thats -2 import-this points already.
>>
>>   
> 
> Which ones are the two?

Explicit is better than implicit.
There should be one-- and preferably only one --obvious way to do it.

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/



More information about the Python-list mailing list