pep 336: Make None Callable

Daniel Fackrell newsgroups.nospam at dfackrell.mailshell.com
Thu Nov 4 15:44:23 EST 2004


"Terry Hancock" <hancock at anansispaceworks.com> wrote in message
news:mailman.5928.1099598880.5135.python-list at python.org...
[snip lots of good stuff]
> Maybe there should be a noop built-in, though?
>
> Cheers,
> Terry

If it were possible, pass() would seem to make sense to me for this purpose,
as it's already a no-op.  Too bad 'pass' is a reserved word and not likely
to be capable of being overloaded in this way.

----
def a():
    return 1

def b():
    return 2

funcs = [a, pass, b]

for func in funcs:
    func()
----

Maybe this comes from my recent wonderings about the separation between
reserved words and callables, though.  I was thinking along the lines that
def and class in particular could be interesting (though not necessarily
useful) with callable versions and hacked together the following:

----
def class_(args, code, name='__anon__'):
    '''Class generator'''
    code = code.split('\n')
    for index in range(len(code)):
        code[index] = '        %s' % code[index]
    code.insert(0, 'class C(%s):' % args )
    code = '\n'.join(code)
    exec(code)
    C.__name__ = name
    return C

def def_(args, code, name='__anon__'):
    '''Function generator'''
    code = code.split('\n')
    for index in range(len(code)):
        code[index] = '        %s' % code[index]
    code.insert(0, 'def F(%s):' % args )
    code = '\n'.join(code)
    exec(code)
    F.__name__ = name
    return F

def lambda_(args, expression):
    return def_(args, 'return %s' % expression, '<lambda>')
----

This has seen little testing, and no production use.

Daniel Fackrell





More information about the Python-list mailing list