[Python-ideas] PEP 505 (None coalescing operators) thoughts

Brian O'Neill twangist at gmail.com
Tue Sep 29 20:04:38 CEST 2015


> 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 []
> 
> 
> Emile

A further virtue of 
    self.vertices = vertices or []
and the like is that they coerce falsy parameters of the wrong type to the falsy object of the correct type.
E.g. if vertices is '' or 0, self.vertices will be set to [], whereas the ternary expression only tests 
for not-None so self.vertices will be set to a probably crazy value.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150929/45714845/attachment-0001.html>


More information about the Python-ideas mailing list