Re: Configuring an object via a dictionary

2024-03-16 Thread dn via Python-list



On 17/03/24 12:06, Peter J. Holzer via Python-list wrote:

On 2024-03-16 08:15:19 +, Barry via Python-list wrote:

On 15 Mar 2024, at 19:51, Thomas Passin via Python-list 
 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
--
https://mail.python.org/mailman/listinfo/python-list


Re: Configuring an object via a dictionary

2024-03-16 Thread Peter J. Holzer via Python-list
On 2024-03-16 08:15:19 +, Barry via Python-list wrote:
> > On 15 Mar 2024, at 19:51, Thomas Passin via Python-list 
> >  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.

hp

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | h...@hjp.at |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Configuring an object via a dictionary

2024-03-16 Thread dn via Python-list

On 16/03/24 21:15, Barry via Python-list wrote:




On 15 Mar 2024, at 19:51, Thomas Passin via Python-list 
 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.


As with any other facility, one has to understand ALL implications!

It must be one of those intensely-frustrating errors to track-down, 
which is then oh-so-simple to fix!


Are you able to list (real, if suitably anonymised) examples of where 
the truthy/falsy was inappropriate, please?


--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: Configuring an object via a dictionary

2024-03-16 Thread Thomas Passin via Python-list

On 3/16/2024 8:12 AM, Roel Schroeven via Python-list wrote:

Barry via Python-list schreef op 16/03/2024 om 9:15:


> On 15 Mar 2024, at 19:51, Thomas Passin via Python-list 
  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.

Me too. It's just too fragile. When writing code you're going to need an 
alternative for cases where "config.get('source_name') or default_value" 
doesn't work correctly; much better to use that alternative for all cases.


Trying to remember when I've used it, that was probably on personal code 
where I had a good idea what the values could be. Otherwise, I'm in 
agreement.


--
https://mail.python.org/mailman/listinfo/python-list


Re: Configuring an object via a dictionary

2024-03-16 Thread Roel Schroeven via Python-list

Barry via Python-list schreef op 16/03/2024 om 9:15:


> On 15 Mar 2024, at 19:51, Thomas Passin via Python-list 
  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.

Me too. It's just too fragile. When writing code you're going to need an 
alternative for cases where "config.get('source_name') or default_value" 
doesn't work correctly; much better to use that alternative for all cases.


--
"This planet has - or rather had - a problem, which was this: most of the
people living on it were unhappy for pretty much of the time. Many solutions
were suggested for this problem, but most of these were largely concerned with
the movement of small green pieces of paper, which was odd because on the whole
it wasn't the small green pieces of paper that were unhappy."
-- Douglas Adams
--
https://mail.python.org/mailman/listinfo/python-list


Re: Configuring an object via a dictionary

2024-03-16 Thread Barry via Python-list


> On 15 Mar 2024, at 19:51, Thomas Passin via Python-list 
>  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.

Barry
-- 
https://mail.python.org/mailman/listinfo/python-list