Nesting Custom Errors in Classes

DL Neil PythonList at DancesWithMice.info
Tue Jul 23 15:21:28 EDT 2019


On 23/07/19 11:55 PM, Cameron Simpson wrote:
> On 23Jul2019 14:54, DL Neil <PythonList at DancesWithMice.info> wrote:
>> Do you use nested classes?
>> Python manages nested classes.
>>
>> I've NEVER seen such 'in the wild'.
>> (but perhaps I lead a sheltered life?)
> 
> So sheltered :-)

Well, over here there aren't the venomous creatures you live amongst on 
'west island'...


> In my experience it's uncommon. I've done it a few times.
> 
> "Nested" classes aren't anything very special; they're no more than yet 
> another name defined inside a class, like a method, or a "constant". Look:


Thanks for the explanation. I've absorbed the concept, but back in the 
?good, old days it was common practice to alter code programmatically, 
ie whilst the pgm was running. The perils of not quite knowing what the 
code 'is' and thus what it should be doing for us, constitute a 
debugging nightmare. (but hey, even flicking toggle-switches on the 
front of an IBM System/360 to load binary data into a specified memory 
address, seemed like 'great power' - at the time *cue SpiderMan quote)

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!)

As you say...

> Personally, I haven't done the above. But I have made subclasses where 
> the subclass overrides a constant, like DEFAULT_FOO_MAX earlier. Then 
> accessing self.DEFAULT_FOO_MAX finds the appropriate constant.
> 
> And on thinking about it, while I haven't explicitly nested classes, I 
> have passed in a class to the init method, for example an indexclass. 
> I've a package which support several backend hash indices, such as gdbm, 
> and I pass in the target index class. So a class as a parameterisable or 
> inheritable thing isn't nonsensical.
> 
> 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!

-- 
Regards =dn



More information about the Python-list mailing list