Where do nested functions live?
Frederic Rentsch
anthra.norell at vtxmail.ch
Tue Oct 31 12:39:04 EST 2006
Rob Williscroft wrote:
> 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.
>
>
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.
> 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.
>
That is certainly a very interesting aspect.
>
>
>> 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 ?
>
>
No. I did a hundred thousand loops over each in IDLE using xrange.
> 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.
>
>
I doubt that very much. The 'outer' keyword would give me the choice
between two alternatives. Such a choice can hardly be automated.
> 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.
>
Frederic
More information about the Python-list
mailing list