On 5/1/2012 5:27, alex23 wrote:
On Apr 30, 2:05 am, Peter Pearson<ppear...@nowhere.invalid>  wrote:
Hey, guys, am I the only one here who can't even guess what
this code does?  When did Python become so obscure?

Thankfully it hasn't. The most Pythonic way to pass around a code
block is still to use a function.

"Most Pythonic" doesn't mean better, unfortunately.

For instance, assume that you want to write a function that accepts a dictionary of callbacks:
  func(some_args, callbacks)

Pythonic way
------------

def when_odd(n):
    pass

def when_prime(n):
    pass

def before_check():
    pass

def after_check():
    pass

func(some_args, {'when_odd' : when_odd,
                 'when_prime' : when_prime,
                 'before_check' : before_check,
                 'after_check' : after_check})

def when_prime(n):
    pass

def when_perfect(n):
    pass

def before_reduction()
    pass

def after_reduction():
    pass

func(some_args, {'when_prime' : when_prime,
                 'when_perfect' : when_perfect,
                 'before_reduction' : before_reduction,
                 'after_reduction' : after_reduction})

My way
------

with func(some_args) << ':dict':
    with when_odd as 'n':
        pass
    with when_prime as 'n':
        pass
    with before_check as '':
        pass
    with after_check as '':
        pass

with func(some_args) << ':dict':
    with when_prime as 'n':
        pass
    with when_perfect as 'n':
        pass
    with before_reduction as '':
        pass
    with after_reduction as '':
        pass

Kiuhnm
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to