简体   繁体   中英

python callbacks vs exceptions in control flow

What is more python for control flow, callbacks or exceptions? For example if i wrote a user login logic i can write it in this ways:

Exceptions

   try:
        valid_data = validate(form)
        try:        
            do_login(valid_data)        
            return SuccessLoginTemplate()
        except LoginError:
            return RenderTemplate(form)
    except FormError:
        return RenderTemplate(form)  

Callbacks:

validate(form, on_form_ok, on_form_error)

def on_form_ok(valid_data):
    do_login(valid_data, on_login_success, on_login_error)


def on_form_error(errors):
    return RenderTemplate(form)

def on_login_success(user):
    return SuccessLoginTemplate()

def on_login_error(errors):
    return RenderTemplate(form)

It seem what most python code writen in 1 case via Exceptions, but IMO callback style more expressive in point of DSL view. In case 2 where is no intermediate code "clue to preapere vars from one calls to another" i mean:

try:
    valid_data = validate(form)
    try:
        #mess with some intermediate vars
        do_login(valid_data)
        #mess with some intermediate vars
        return SuccessLoginTemplate()
    except LoginError:
        #mess with some intermediate vars
        return RenderTemplate(form)
except FormError:
    #mess with some intermediate vars
    return RenderTemplate(form)

IMO this mess with intermediate vars reduce readability of code, beacause in case of callbacks intermediate code goes to callback and it more easier to understand it then it wraped in function, so it gets some context in it (i mean function name which carry some DSL semantic) and in case of exception this code unbound from any context, so it harder to read)

Also I dont't want to check results of functions - this also make mess with some intermediate vars. I search for clearenest way to chaining computation together in more functional style, like monads or CPS style. So IMO callbacks are more expresive way to do it, but how pythonic is it?

IMO, you have 3 obvious contexts: 1 try and 2 distinct except s with all variables and stuff explicitly manipulated and passed while with callbacks you don't know from (unless you write docs or anything like this) expression at least what are the arguments for the callbacks you're passing. Exception block with exception name provide enough meaning to the code block. And you even have to implement validate like this (with callbacks)

def validate(data, on_success, on_error1, on_error2):
    try:
        # do_validation_stuff here
    except Exception1:
        on_error1()
    except Exception2:
        on_error2()
    else:
        on_success()

thus validate function not only do validation, but handle error processing, and in your example it do user login.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM