[Python-ideas] why not try without except?

Steven D'Aprano steve at pearwood.info
Sun Apr 26 13:38:00 CEST 2009


On Sun, 26 Apr 2009 07:52:40 pm spir wrote:

> Mine is: optional things and optional actions are basic and common
> notions. Even more in modelizing / programming fields. Are they
> important enough (for you) to require a dedicated idiom in a (your
> favorite) PL?
>
> class Shape(object):
>    def __init__(self, ..., ?fill_color)
>       .......
>       ?self.fill_color = fill_color
>    def show(self):
>       .......
>       ?self.fill(self.fill_color)

In this case, fill_colour shouldn't be optional. There are two better 
ways to deal with it. Here's one:

class UnfilledShape(object):  # no fill colour at all.
    ...

class FilledShape(UnfilledShape):  # has a fill colour
    ...



or if you prefer:

class Shape(object):
    def __init__(self, ..., fill_color=TRANSPARENT):
        ...


Either solution is better than writing code that is filled with checks 
for missing values. Whether you manually check it yourself, or have 
syntax support, you still make the code more complicated.



-- 
Steven D'Aprano



More information about the Python-ideas mailing list