Sujet : Re: Configuring an object via a dictionary
De : PythonList (at) *nospam* DancesWithMice.info (dn)
Groupes : comp.lang.pythonDate : 17. Mar 2024, 06:15:32
Autres entêtes
Organisation : DWM
Message-ID : <mailman.106.1710648947.3452.python-list@python.org>
References : 1 2 3 4
User-Agent : Mozilla Thunderbird
On 17/03/24 12:06, Peter J. Holzer via Python-list wrote:
On 2024-03-16 08:15:19 +0000, Barry via Python-list wrote:
On 15 Mar 2024, at 19:51, Thomas Passin via Python-list <python-list@python.org> wrote:
I've always like writing using the "or" form and have never gotten bit
>
I, on the other hand, had to fix a production problem that using “or” introducted.
I avoid this idiom because it fails on falsy values.
Perl has a // operator (pronounced "err"), which works like || (or),
except that it tests whether the left side is defined (not None in
Python terms) instead of truthy. This still isn't bulletproof but I've
found it very handy.
So, if starting from:
def method( self, name=None, ):
rather than:
self.name = name if name else default_value
ie
self.name = name if name is True else default_value
the more precise:
self.name = name if name is not None or default_value
or:
self.name = default_value if name is None or name
because "is" checks for identity, whereas "==" and True-thy encompass a range of possible alternate values?
-- Regards,=dn