[Python-ideas] PEP 505 (None coalescing operators) thoughts
Emile van Sebille
emile at fenx.com
Tue Sep 29 22:50:43 CEST 2015
Thanks -- I think I've got a better handle now on the why of this
discussion.
Emile
On 9/29/2015 1:33 PM, Andrew Barnert via Python-ideas wrote:
> On Sep 29, 2015, at 09:58, Emile van Sebille <emile at fenx.com> wrote:
>>
>>> On 9/29/2015 9:20 AM, Rob Cliffe wrote:
>>> Why not
>>>
>>> def __init__(self, vertices=None, edges=None, weights=None,
>>> source_nodes=None):
>>> self.vertices = vertices if vertices is not None else []
>>> self.edges = edges if edges is not None else []
>>> self.weights = weights if weights is not None else {}
>>> self.source_nodes = source_nodes if source_nodes is not None else []
>>
>> I don't understand why not:
>>
>> self.vertices = vertices or []
>> self.edges = edges or []
>> self.weights = weights or {}
>> self.source_nodes = source_nodes or []
>
> Because empty containers are just as falsey as None.
>
> So, if I pass in a shared list, your "vertices or []" will replace it with a new, unshared list; if I pass a tuple because I need an immutable graph, you'll replace it with a mutable list; if I pass in a blist.sortedlist, you'll replace it with a plain list. Worse, this will only happen if the argument I pass happens to be empty, which I may not have thought to test for.
>
> This is the same reason you don't use "if spam:" when you meant "if spam is not None:", which is explained in PEP 8.
>
> Also, I believe the PEP for ternary if-else explains why this is an "attractive nuisance" misuse of or, as one of the major arguments for why a ternary expression should be added.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
More information about the Python-ideas
mailing list