Question

In python I can create a class without class statement:

MyClass = type('X', (object,), dict(a=1))

Is there a way to create a function without 'def'? Thats as far as i got...

d={} # func from string
exec'''\
def synthetics(s):
    return s*s+1
''' in d

>>> d.keys()
['__builtins__', 'synthetics']
>>> d['synthetics']
<function synthetics at 0x00D09E70>
>>> foo = d['synthetics']
>>> foo(1)
2
Was it helpful?

Solution

Technically, yes, this is possible. The type of a function is, like all other types, a constructor for instances of that type:

FunctionType = type(lambda: 0)
help(FunctionType)

As you can see from the help, you need at minimum code and globals. The former is a compiled bytecode object; the latter is a dictionary.

To make the code object, you can use the code type's constructor:

CodeType = type((lambda: 0).func_code)
help(CodeType)

The help says this is "not for the faint of heart" and that's true. You need to pass bytecode and a bunch of other stuff to this constructor. So the easiest way to get a code object is from another function, or using the compile() function. But it is technically possible to generate code objects completely synthetically if you understand Python bytecode well enough. (I have done this, on a very limited basis, to construct signature-preserving wrapper functions for use in decorators.)

PS -- FunctionType and CodeType are also available via the types module.

OTHER TIPS

There might be a more direct way than the following, but here's a full-blown function without def. First, use a trivial lambda expression to get a function object:

>>> func = lambda: None

Then, compile some source code to get a code object and use that to replace the lambda's code:

>>> func.__code__ = compile("print('Hello, world!')", "<no file>", "exec")
>>> func()
Hello, world!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top