Configuring an object via a dictionary

dn PythonList at DancesWithMice.info
Sun Mar 17 17:09:27 EDT 2024


On 18/03/24 04:11, Peter J. Holzer via Python-list wrote:
> On 2024-03-17 17:15:32 +1300, dn via Python-list wrote:
>> 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 at 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
> 
> These two lines don't have the same meaning (for the reason you outlined
> below). The second line is also not very useful.
> 
> 
> 
>> 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
> 
> Those are syntax errors. I think you meant to write "else" instead of
> "or".
> 
> Yes, exactly. That's the semantic of Perl's // operator.
> 
> JavaScript has a ?? operator with similar semantics (slightly
> complicated by the fact that JavaScript has two "nullish" values).


Thanks Peter!
(yes, sad consequences of suffering a neighbor's party-til-midnight 
followed by an 0530 meeting-start - children: don't code exhausted!)


Herewith, an illustration of how the corrected version of the above 
works - and how (in what seem unusual cases) it avoids any truthy/falsy 
confusion, as raised earlier in this thread:

 >>> default_value = "default"
 >>> name = "Fred Flintstone"
 >>> name if name is not None else default_value
'Fred Flintstone'

 >>> name = None
 >>> name if name is not None else default_value
'default'
 >>> name = False
 >>> name if name is not None else default_value
False
 >>> name = 1
 >>> name if name is not None else default_value
1
 >>> name = 0
 >>> name if name is not None else default_value
0


Personally: I find the above coding more logical, because our primary 
interest is on 'the happy case', ie where the value has been assigned 
(to "name"); and the default_value is only applied as a "guard".


On the other hand, I dislike the not-condition because it forces me to 
think (and maybe dust-off DeMorgan). Accordingly:

 >>> name = "Fred Flintstone"
 >>> default_value if name is None else name
'Fred Flintstone'
 >>> name = None
 >>> default_value if name is None else name
'default'
 >>> name = False
 >>> default_value if name is None else name
False
...


YMMV!
NB your corporate Style Guide may prefer 'the happy path'...

-- 
Regards,
=dn


More information about the Python-list mailing list