1

I am trying to write a function that can define functions.

I tried this:

def function_writer():
    print "I am a function writer"  

    def new_function():
        print "I am a new function"
             pass
    pass

function_writer()
new_function()

However, my debugger complains that new_function is not defined and so this obviously is not the way to do it.

Does anyone know how to do this?

Thanks.

  • I'm still confused by the wording of this question: how can a function "write a function"? – Anderson Green Nov 06 '13 at 23:37
  • @AndersonGreen: "write", "create", ... – Steve Jessop Nov 06 '13 at 23:42
  • @SteveJessop "write" could also mean "print", I suppose: http://stackoverflow.com/questions/15297413/how-to-print-the-python-code-of-a-function-to-the-terminal – Anderson Green Nov 06 '13 at 23:45
  • @AndersonGreen: agreed. I'm just saying that what `function_writer` actually does is create a function, so let's hope that's what the questioner means by "write" :-) I suppose that to someone coming from a language where functions cannot be dynamically created, the only way a function exists is because you (the programmer) have written it. This might be the reason for conflating the two concepts. – Steve Jessop Nov 06 '13 at 23:47
  • I changed it to defining instead of writing – CodeHard_or_HardCode Nov 06 '13 at 23:56

5 Answers5

5

Your code fails for for exactly the same reason that the following code complains that i is not defined:

def integer_writer():
    i = 0

integer_writer()
i

i is a local variable in the function integer_writer, and new_function is a local variable in the function function_writer.

You could do the following:

def function_writer():
    print "I am a function writer"  

    def new_function():
        print "I am a new function"

    return new_function

new_function = function_writer()
new_function()

In this case there doesn't seem to be much point (just as there wouldn't normally be much point defining a function that always returns 0), but if function_writer were to take some parameters, and if those parameters were used in the function it returns, then you'd be in business.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • To elaborate further on this, you can also do `function_writer()()` to both call the function that creates the function, then immediately call the returned function... you don't have to give it a name. – kindall Nov 06 '13 at 23:44
  • You're right, I totally forgot the function would be localized. thanks. – CodeHard_or_HardCode Nov 06 '13 at 23:54
3
def function_writer():
    print "I am a function writer"  

    def new_function():
        print "I am a new function"

    return new_function

fn_dynamic = function_writer()
fn_dynamic() #call new_function

maybe what you are looking for? (this is almost what decorators do ... except they modify a passed in method)

another solution (this is not a good suggestion but answers the question)

def function_writer():
    print "I am a function writer"      
    def new_function():
        print "I am a new function"
    globals()['new_function'] = new_function

function_writer()
new_function()

this type of functionality is typically avoided in python unless you have a VERY COMPELLING reason why this is absolutely what you need ...

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • close but now it says "TypeError: 'NoneType' object is not callable" – CodeHard_or_HardCode Nov 06 '13 at 23:46
  • Yeah it works now. That's cool I didn't know you could assign an object to a function and then call it like that. – CodeHard_or_HardCode Nov 06 '13 at 23:53
  • To be more general, they "do something" with the passed in method/function. That my include calling it and return its result, iterating over it and returning the resulting joined string, or, as you said, modifying it by creating a wrapper which, in turn, calls it. – glglgl Nov 07 '13 at 07:10
1

Of course it will result in an error because new_function is limited to function_writer scope.

def function_writer():
    print "I am a function writer"  

    def new_function():
        print "I am a new function"
        pass

    return new_function

a = function_writer()
#output: "I am a function writer"

a()
#output: "I am a new function"
1

It depends what you plan to do with it.

  1. You can call it inside the function.

    That may be the case if the inner function needs to access local variables of the outer one.

  2. You can return it in order to be called from outside.

    That is quite usual e. g. in the case of decorators.

Community
  • 1
  • 1
glglgl
  • 89,107
  • 13
  • 149
  • 217
1

If you want to create a function factory, just return the created function!

def factory():
    def new_func():
        print 'Hello!'
  return new_func

f = factory()
f()
Michael Clerx
  • 2,928
  • 2
  • 33
  • 47