“Ask forgiveness not permission” - explain

前端 未结 8 1595
长情又很酷
长情又很酷 2020-11-22 02:02

I\'m not asking for personal \"religious\" opinions about this philosophy, rather something a bit more technical.

I understand this phrase is one of several litmus t

8条回答
  •  灰色年华
    2020-11-22 02:18

    There's lots of good answers here, I just thought I would add a point I have not seen mentioned so far.

    Often asking for forgiveness instead of permission has performance improvements.

    • When you ask for permission, you have to perform an extra operation to ask for permission every time.
    • When asking for forgiveness, you only have to perform an extra operation sometimes, i.e. when it fails.

    Usually the failure cases are rare, which means if you are only asking for permission, then you hardly ever have to do any extra operations. Yes, when it fails it throws an exception, as well as performing an extra operation, but exceptions in python are very fast. You can see some timings here: https://jeffknupp.com/blog/2013/02/06/write-cleaner-python-use-exceptions/

提交回复
热议问题