internal functions [Re: How do you feel ?]

Scott David Daniels Scott.Daniels at Acm.Org
Wed Aug 25 17:07:43 CEST 2004


Howard Stearns wrote:

> Sorry. When I said "define a named function in a top-level assignment", 
> I didn't just mean using a reference to a previously defined function 
> (i.e., it's name), I meant actually defining the function in the 
> assignment.
> 
> If you haven't done this sort of thing for years, I could see it being 
> hard to imagine why I would want to do this. After all, isn't the code 
> clearer if we define the function with a name ahead of time? And doesn't 
> the debugger work better with functions that have a name?
> 
> Well, I agree with this for what might be called middle-ground uses. 
> However, I do a lot of work with very simple cases and very complicated 
> ones.
> 
> In very simple cases, the code is more cluttered if I have to define the 
> function separately. Now, when the function is simple, Python let's me 
> define an anonymous function in the assignment. That's cool. But I also 
> have cases where the assignment is simple, but maybe the function isn't. 
> This came up for me when I tried to populate generic functions with 
> methods. (See generic functions thread. Though it looks like decorators 
> will soon help me out here...)
> 
> In very complex cases, I lament the shear distance between the function 
> definition and the one place in the code where it is referenced. (Again, 
> maybe decorators will help. Cool.)
> 
> 
> 
> Peter Hansen wrote:
> 
>> Howard Stearns wrote:
>>
>>> The one thing I've found annoying is that I haven't yet discovered 
>>> how to do whatever I want in lambda expressions. I have top-level 
>>> assignments where I'd like create a function to use as the the value 
>>> being assigned. I don't know how to define a named function in a 
>>> top-level assignment, and a lambda won't allow me to use 'try' and 
>>> other statements -- just expressions. Or am I looking at things wrong?
>>
>>
>>
>> What do you mean by "top-level assignment"?  If it's the same
>> meaning most Python programmers would give it, you just do this:
>>
>> def somefunc():
>>     pass
>>
>> topLevelName = somefunc
>>
>> There's your top-level assignment of a named function.  Probably
>> not what you meant, but can you clarify please?
>>
>> -Peter
> 
> 
Maybe this is what you mean:

def dodef(val):
     global globfunc
     def globfunc(other):
         return val, other

This works just fine.

-Scott David Daniels
Scott.Daniels at Acm.Org




More information about the Python-list mailing list