Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Ben Finney Newsgroups: comp.lang.python Subject: EAFP and LBYL (was: Try: rather than if :) Date: Tue, 15 Dec 2015 10:19:38 +1100 Lines: 25 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de BWlp9vmzCxZPbUqoiE3E8wVIfh4OHk+AIzebV/+ozXuw== Cancel-Lock: sha1:sNsXBmLAej2eosRQZn98F41NUl4= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'used.': 0.05; 'url:pycon': 0.08; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tends': 0.09; '\xe2\x80\x94': 0.09; 'bug': 0.10; 'python': 0.10; 'appropriate': 0.14; 'another?': 0.16; 'attributed': 0.16; 'explores': 0.16; 'forgiveness': 0.16; 'hopper': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'alex': 0.18; 'attribute': 0.18; 'documented': 0.18; 'refers': 0.18; 'programming': 0.22; 'seems': 0.23; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'actual': 0.28; 'thinks': 0.29; 'code': 0.30; 'statement': 0.32; 'url:python': 0.33; 'handle': 0.34; 'could': 0.35; 'robert': 0.35; 'community': 0.36; 'there': 0.36; 'url:org': 0.36; 'possible': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'difference': 0.38; 'to:addr:python.org': 0.40; '_o__)': 0.84; 'legend': 0.84; 'received:125': 0.84; 'url:2007': 0.84; 'rear': 0.91; 'url:video': 0.91; '\xe2\x80\x9cthe': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100436 Vincent Davis writes: > In the code below try is used to check if handle has the attribute name. It > seems an if statement could be used. Is there reason one way would be > better than another? The Python community refers to the difference by contrasting “look before you leap” (LBYL) versus “easier to ask forgiveness than permission” (EAFP), and tends to prefer the latter. “It is easier to ask forgiveness than permission” is attributed to computer programming legend Rear Admiral Grace Hopper (she who documented the first actual computer bug — a large moth in the wires). Alex Martelli explores when LBYL and EAFP are each appropriate in Python . -- \ “The optimist thinks this is the best of all possible worlds. | `\ The pessimist fears it is true.” —J. Robert Oppenheimer | _o__) | Ben Finney