Where do nested functions live?

F

Frederic Rentsch

Rob said:
Frederic Rentsch wrote in (e-mail address removed) in comp.lang.python:

Rob said:
Frederic Rentsch wrote in
Rob Williscroft wrote:

Frederic Rentsch wrote in
[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
 
S

Steve Holden

Frederic said:
Rob said:
Frederic Rentsch wrote in (e-mail address removed) in comp.lang.python:


Rob Williscroft wrote:


Frederic Rentsch wrote in

Rob Williscroft wrote:


Frederic Rentsch wrote in
[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
 
R

Rob Williscroft

Frederic Rentsch wrote in (e-mail address removed) in comp.lang.python:
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?

Because you hadn't yet pointed this out to me :).

Thanks

Rob.
 
R

Rob Williscroft

Steve Holden wrote in
in
comp.lang.python:
Since we have a class that goes out of scope
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.

What problems would it have that a recursive global function
that modified a global object, or a recursive method that
modified its instance doesn't have ?

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top