Welcome, guest | Sign In | My Account | Store | Cart

A simple block function that lets one send multi line blocks of code to a function it doesn't really act like a normal def/lambda but offers cool style

Python, 30 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'''
@author: Yoav Glazner
'''


def block(code):
    code = 'def anon(*args, **kw):\n'+'\n'.join(
                    "\t\t%s" % line for line in code.split('\n') if line.strip())
    env = {}
    exec code in env
    return env['anon'] #@UndefinedVariable suppressed


if __name__ == '__main__':
    from threading import Thread
    
    t = Thread(target=block('''\
    print 'pop'
    print 'pop2'
    '''))
    p, c = 'pop', 'corn'
    t2 = Thread(target=block('''\
    a, b = args
    print a + b
    '''), args=(p, c))
    
    t.start()
    t2.start()
    t.join()
    t2.join()
Created by yoav glazner on Mon, 24 Oct 2011 (PSF)
Python recipes (4591)
yoav glazner's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks