Nesting Custom Errors in Classes

Cameron Simpson cs at cskk.id.au
Tue Jul 23 18:07:33 EDT 2019


On 24Jul2019 07:21, DL Neil <PythonList at DancesWithMice.info> wrote:
>Accordingly, the idea that there would be a ClassyThing() but it might 
>be quite different from another ClassyThing() just makes me shudder!
>(sorry, I guess it's that sheltered upbringing ... again!)

It needn't be very different on the outside. The whole point of object 
oriented programme is to present things with the same external API but 
different internal implementations, or managing a different thing with 
common characteristics.

>>I have use nested classes when the nested class is strongly associated 
>>with the enclosing class, like your classname.exceptionname example. 
>>I'd put it inside the outer class entirely to express that 
>>association.
>
>...and, because the (?old) boy can't be relied upon to remember to 
>import both, nesting saves-the-day!

Get some linting tools. They're great for catching this kind of error.

The tricky bit with dynamic language like Python is that some naming 
errors (missed imports) aren't apparent until the code passes through 
that path. Linters can cover a lot of this with static analysis.

Cheers,
Cameron Simpson <cs at cskk.id.au>



More information about the Python-list mailing list