Re: Where do nested functions live?

2006-11-01 Thread Rob Williscroft
Frederic Rentsch wrote in news:mailman.1556.1162316571.11739.python- [EMAIL PROTECTED] 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-

Re: Where do nested functions live?

2006-11-01 Thread Carl Banks
. where do nested functions live? They live inside the scope of the function. Inaccessible from outside, Not so fast. You can get at the nested function by peeking inside code objects (all bets off for Pythons other than CPython). import new def extract_nested_function(func,name): codetype

Re: Where do nested functions live?

2006-11-01 Thread Fredrik Lundh
Carl Banks wrote: Not so fast. You can get at the nested function by peeking inside code objects (all bets off for Pythons other than CPython). import new def extract_nested_function(func,name): codetype = type(func.func_code) for obj in func.func_code.co_consts: if

Re: Where do nested functions live?

2006-11-01 Thread Frederic Rentsch
Rob Williscroft wrote: Frederic Rentsch wrote in news:mailman.1556.1162316571.11739.python- [EMAIL PROTECTED] in comp.lang.python: Rob Williscroft wrote: Frederic Rentsch wrote in news:mailman.1536.1162292996.11739.python- Rob Williscroft wrote: Frederic Rentsch

Re: Where do nested functions live?

2006-11-01 Thread Steve Holden
Frederic Rentsch wrote: Rob Williscroft wrote: Frederic Rentsch wrote in news:mailman.1556.1162316571.11739.python- [EMAIL PROTECTED] in comp.lang.python: Rob Williscroft wrote: Frederic Rentsch wrote in news:mailman.1536.1162292996.11739.python- Rob Williscroft wrote:

Re: Where do nested functions live?

2006-11-01 Thread Rob Williscroft
Frederic Rentsch wrote in news:mailman.1613.1162403556.11739.python- [EMAIL PROTECTED] 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?

Re: Where do nested functions live?

2006-11-01 Thread Rob Williscroft
Steve Holden wrote in news:[EMAIL PROTECTED] 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? def whatever( new_ms ): class scope

Re: Where do nested functions live?

2006-10-31 Thread Frederic Rentsch
Rob Williscroft wrote: Frederic Rentsch wrote in news:mailman.1428.1162113628.11739.python- [EMAIL PROTECTED] in comp.lang.python: def increment_time (interval_ms): outer weeks, days, hours, minutes, seconds, mseconds # 'outer' akin to 'global' (...) mseconds =

Re: Where do nested functions live?

2006-10-31 Thread Rob Williscroft
Frederic Rentsch wrote in news:mailman.1536.1162292996.11739.python- [EMAIL PROTECTED] in comp.lang.python: Rob Williscroft wrote: Frederic Rentsch wrote in news:mailman.1428.1162113628.11739.python- [EMAIL PROTECTED] in comp.lang.python: def whatever( new_ms ): class namespace(

Re: Where do nested functions live?

2006-10-31 Thread Frederic Rentsch
Rob Williscroft wrote: Frederic Rentsch wrote in news:mailman.1536.1162292996.11739.python- [EMAIL PROTECTED] in comp.lang.python: Rob Williscroft wrote: Frederic Rentsch wrote in news:mailman.1428.1162113628.11739.python- [EMAIL PROTECTED] in comp.lang.python:

Re: Where do nested functions live?

2006-10-30 Thread Rob Williscroft
Frederic Rentsch wrote in news:mailman.1428.1162113628.11739.python- [EMAIL PROTECTED] in comp.lang.python: def increment_time (interval_ms): outer weeks, days, hours, minutes, seconds, mseconds # 'outer' akin to 'global' (...) mseconds = new_ms - s * 1000#

Re: Where do nested functions live?

2006-10-29 Thread Frederic Rentsch
Diez B. Roggisch wrote: If I may turn the issue around, I could see a need for an inner function to be able to access the variables of the outer function, the same way a function can access globals. Why? Because inner functions serve to de-multiply code segments one would otherwise need to

Re: Where do nested functions live?

2006-10-29 Thread Fredrik Lundh
Frederic Rentsch wrote: At some later point I need to increment my units some more and probably will again a number of times. Clearly this has to go into a function. since Python is an object-based language, clearly you could make your counter into a self-contained object instead of writing

Re: Where do nested functions live?

2006-10-29 Thread Frederic Rentsch
Fredrik Lundh wrote: Frederic Rentsch wrote: At some later point I need to increment my units some more and probably will again a number of times. Clearly this has to go into a function. since Python is an object-based language, clearly you could make your counter into a

Where do nested functions live?

2006-10-28 Thread Steven D'Aprano
no attribute 'bar' but it doesn't work as I expected. where do nested functions live? How can you access them, for example, to read their doc strings? -- Steven. -- http://mail.python.org/mailman/listinfo/python-list

Re: Where do nested functions live?

2006-10-28 Thread Fredrik Lundh
, in ? AttributeError: 'function' object has no attribute 'bar' but it doesn't work as I expected. where do nested functions live? in the local variable of an executing function, just like the variable bar in the following function: def foo(): bar = who am I? where do I live? (yes, an inner

Re: Where do nested functions live?

2006-10-28 Thread Ben Finney
, in ? AttributeError: 'function' object has no attribute 'bar' but it doesn't work as I expected. Functions don't get attributes automatically added to them the way class do. The main exception is the '__doc__' attribute, referring to the doc string value. where do nested functions live

Re: Where do nested functions live?

2006-10-28 Thread Steve Holden
, in ? AttributeError: 'function' object has no attribute 'bar' but it doesn't work as I expected. where do nested functions live? How can you access them, for example, to read their doc strings? It doesn't live anywhere: if I wrote the function def foo(): locvar = 23 return locvar would

Re: Where do nested functions live?

2006-10-28 Thread Fredrik Lundh
Ben Finney wrote: If you want something that can be called *and* define its attributes, you want something more complex than the default function type. Define a class that has a '__call__' attribute, make an instance of that, and you'll be able to access attributes and call it like a

Re: Where do nested functions live?

2006-10-28 Thread Steven D'Aprano
On Sat, 28 Oct 2006 09:59:29 +0200, Fredrik Lundh wrote: where do nested functions live? in the local variable of an executing function, just like the variable bar in the following function: def foo(): bar = who am I? where do I live? (yes, an inner function is *created

Re: Where do nested functions live?

2006-10-28 Thread Marc 'BlackJack' Rintsch
In [EMAIL PROTECTED], Steven D'Aprano wrote: Does this mean I'm wasting my time writing doc strings for nested functions? If there is no way of accessing them externally, should I make them mere # comments? Whats the difference in wasted time between using or # as delimiters for the

Re: Where do nested functions live?

2006-10-28 Thread Andrea Griffini
Fredrik Lundh wrote: Ben Finney wrote: If you want something that can be called *and* define its attributes, you want something more complex than the default function type. Define a class that has a '__call__' attribute, make an instance of that, and you'll be able to access attributes and

Re: Where do nested functions live?

2006-10-28 Thread Frederic Rentsch
): File stdin, line 1, in ? AttributeError: 'function' object has no attribute 'bar' but it doesn't work as I expected. where do nested functions live? in the local variable of an executing function, just like the variable bar in the following function: def foo

Re: Where do nested functions live?

2006-10-28 Thread Diez B. Roggisch
If I may turn the issue around, I could see a need for an inner function to be able to access the variables of the outer function, the same way a function can access globals. Why? Because inner functions serve to de-multiply code segments one would otherwise need to repeat or to provide a