What is the EAFP principle in Python?

前端 未结 3 1461
自闭症患者
自闭症患者 2020-11-21 22:44

What is meant by \"using the EAFP principle\" in Python? Could you provide any examples?

3条回答
  •  一向
    一向 (楼主)
    2020-11-21 23:09

    I'll try to explain it with another example.

    Here we're trying to access the file and print the contents in console.

    LBYL - Look Before You Leap :

    We might want to check if we can access the file and if we can, we'll open it and print the contents. If we can't access the file we'll hit the else part. The reason that this is a race condition is because we first make an access-check. By the time we reach with open(my_file) as f: maybe we can't access it anymore due to some permission issues (for example another process gains an exclusive file lock). This code will likely throw an error and we won't be able to catch that error because we thought that we could access the file.

    import os
    
    my_file = "/path/to/my/file.txt"
    
    # Race condition
    if os.access(my_file, os.R_OK):
        with open(my_file) as f:
            print(f.read())
    else:
        print("File can't be accessed")
    

    EAFP - Easier to Ask for Forgiveness than Permission :

    In this example, we're just trying to open the file and if we can't open it, it'll throw an IOError. If we can, we'll open the file and print the contents. So instead of asking something we're trying to do it. If it works, great! If it doesn't we catch the error and handle it.

    # # No race condition
    try:
        f = open(my_file)
    except IOError as e:
        print("File can't be accessed")
    else:
        with f:
            print(f.read())
    

提交回复
热议问题