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.

5
  • I'm still confused by the wording of this question: how can a function "write a function"? Commented Nov 6, 2013 at 23:37
  • @AndersonGreen: "write", "create", ... Commented Nov 6, 2013 at 23:42
  • @SteveJessop "write" could also mean "print", I suppose: stackoverflow.com/questions/15297413/… Commented Nov 6, 2013 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. Commented Nov 6, 2013 at 23:47
  • I changed it to defining instead of writing Commented Nov 6, 2013 at 23:56

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
You're right, I totally forgot the function would be localized. thanks.
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 ...

3 Comments

close but now it says "TypeError: 'NoneType' object is not callable"
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.
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.
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"

Comments

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.

Comments

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()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.