Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Lack of skill dictates economy of style. -- Joey Ramone


devel / comp.lang.python / Re: EAFP

SubjectAuthor
o Re: EAFPdn

1
Re: EAFP

<mailman.410.1652504746.20749.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18271&group=comp.lang.python#18271

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: PythonL...@DancesWithMice.info (dn)
Newsgroups: comp.lang.python
Subject: Re: EAFP
Date: Sat, 14 May 2022 17:05:19 +1200
Organization: DWM
Lines: 61
Message-ID: <mailman.410.1652504746.20749.python-list@python.org>
References: <dr0rik5hru4d406b3c66jpvu.1652459869283@email.android.com>
<66493f9c-ec99-05d6-9e3f-f80270f0ac22@DancesWithMice.info>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de n8JwF9/38fofGuqBJwkkvQ58dRedQZjyb6mXrvaNw75w==
Return-Path: <PythonList@DancesWithMice.info>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=danceswithmice.info header.i=@danceswithmice.info
header.b=eZfMMo5h; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.036
X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'def': 0.04; '=dn': 0.09;
'else:': 0.09; 'from:addr:danceswithmice.info': 0.09;
'from:addr:pythonlist': 0.09; 'problem.': 0.15; '(once': 0.16;
'by,': 0.16; 'message-id:@DancesWithMice.info': 0.16;
'received:51.254': 0.16; 'received:51.254.211': 0.16;
'received:51.254.211.219': 0.16; 'received:cloud': 0.16;
'received:rangi.cloud': 0.16; 'values,': 0.16; 'wrote:': 0.16;
'larger': 0.17; 'values': 0.17; 'to:addr:python-list': 0.20;
'exception': 0.22; 'code': 0.23; '(and': 0.25; 'leave': 0.27;
'expect': 0.28; 'header:User-Agent:1': 0.30; 'raise': 0.31;
'header:Organization:1': 0.31; 'there': 0.33; 'header:In-Reply-
To:1': 0.34; 'automatic': 0.35; 'following': 0.35; 'change': 0.36;
"skip:' 10": 0.37; 'class': 0.37; 'received:192.168': 0.37;
'could': 0.38; 'means': 0.38; 'valid': 0.39; 'decide': 0.39;
'define': 0.40; 'exact': 0.40; 'to:': 0.62; 'skip:z 10': 0.62;
'clear': 0.64; 'received:51': 0.64; 'specifics': 0.64; 'thus':
0.64; 'received:userid': 0.66; 'now,': 0.67; 'respond': 0.67;
'attached': 0.68; 'raised': 0.70; 'vary': 0.76; 'yes': 0.76;
'that...': 0.84; 'samsung': 0.91; 'implied': 0.93
DKIM-Filter: OpenDKIM Filter v2.11.0 vps.rangi.cloud D065A3459
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=danceswithmice.info;
s=staff; t=1652504743;
bh=CoJ2y/DR6CwSAqR9n0OyoeqSa9YH4rNVV+cE1NZI1/g=;
h=Date:Subject:To:References:From:In-Reply-To:From;
b=eZfMMo5hQ0PjBErozUCM7jNJoUB360aRdsgF+EOjzqU+KJDMr6ylyFIDAaS840ZGW
vucR8eTPs60fkEGuoSddm0EoKa1hOb8jLYSmSomDMp3kpG154mNwOf7Y0B6HzE9+uV
160UyYY6GQ7Ryo97QcN9Dz1DVYtJarFkRnHGxWYpVpFdXpF19swF5Iv2VzAlxp6/pr
3Gu9FvXor8nsuERVFx/G4xL1DhznumlJSN8eihQuVxhXBBn9ilk705u2t+19gc8jI3
UmCTxg2OKgohG+Ki+7lNy9Fj6EQriWE5srSa8lRdbWoYHNt8Yvm1NPPFohNQv7I3Wr
9QX2DbSogOh8Q==
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on vps517507.ovh.net
X-Spam-Level:
X-Spam-Status: No, score=-5.9 required=5.0 tests=ALL_TRUSTED,BAYES_00,
DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,NICE_REPLY_A,T_SCC_BODY_TEXT_LINE
autolearn=ham autolearn_force=no version=3.4.0
DKIM-Filter: OpenDKIM Filter v2.11.0 vps.rangi.cloud 0B39C33E6
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=danceswithmice.info;
s=staff; t=1652504742;
bh=CoJ2y/DR6CwSAqR9n0OyoeqSa9YH4rNVV+cE1NZI1/g=;
h=Date:Subject:To:References:From:In-Reply-To:From;
b=fTOlMXE85qP0F873iKcV2Z+1quqHKu5K+iDPziVmhNStBzhXQJjey1kvii9J7dsD2
OaENCkk0vmylWSxUubgMt9Pp8csQkvmxu2poaCfM7uQXp9ecfPin1HtTZvuBzUXkLp
6qbBBdk08KOvCYQMg5FGb8TdHmKi7vh+EmvTxPWkFIOQqh3GODXDav09HMnrv0gv4g
a8EqlJHnO58R22deX9EYKCxDN9qFhtm1sSm717l9iPZENzxK2RjANOMHa2GuoybOFq
STWw2p6kQtpAn7cYwlLscXO86v4U9YoidlagU8Qqj8xWBqvPLjA4OISEL0+JXhoEnA
xd6m3hJ/we6lQ==
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
Content-Language: en-GB
In-Reply-To: <dr0rik5hru4d406b3c66jpvu.1652459869283@email.android.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <66493f9c-ec99-05d6-9e3f-f80270f0ac22@DancesWithMice.info>
X-Mailman-Original-References: <dr0rik5hru4d406b3c66jpvu.1652459869283@email.android.com>
 by: dn - Sat, 14 May 2022 05:05 UTC

On 14/05/2022 04.37, bryangan41 wrote:
> Is the following LBYL:foo = 123if foo < 200:    do()

Yes (once formatted for Python).

If so, how to change to EAFP?

Not sure if can. Let's alter the code to:

foo = 0

#and

def do():
return 5 / foo

Then, you will expect a ZeroDivisionError exception to be raised -
whereas with other values, there will (likely) be no problem.

Now, we have a clear identification for when 'forgiveness' will need to
be requested! The 'EAFP' solution encloses the function-call:

foo = 123
try:
do()
except ZeroDivisionError:
undo()

In the OP's code-snippet, the "200" criteria means there is no such
clean-cut and automatic 'failure' attached to, or implied by, foo's
value. However, we could define a custom-exception and 'raise the alarm'
when 'forgiveness' is required:

class ValueTooLargeError( ValueError):
"""Catch values larger than the valid range."""

def do():
if foo < 200:
...
else:
raise ValueTooLargeError

This is a pythonic-construct (and thus a recommendable coding-pattern),
in that the calling-routine can decide how to respond to the exception -
which many vary according to the specifics of do()'s re-use.

However, is it "EAFP"? We had to introduce the exact if-condition which
makes it "LBYL"!

Perhaps time for me to bow-out, and leave such to the philosophers...

Sent from Samsung tablet.

There are pills for that...
--
Regards,
=dn

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor