|
|
Subscribe / Log in / New account

Python attributes, __slots__, and API design

Python attributes, __slots__, and API design

Posted Jul 27, 2021 18:14 UTC (Tue) by sammythesnake (guest, #17693)
In reply to: Python attributes, __slots__, and API design by bluss
Parent article: Python attributes, __slots__, and API design

The problem is that - properties or not - there's no way for the class to spot a mistyped attempt at using a valid attribute name without using __slots__ or overriding __setattr__ or the like (and bring able to extend that class with a subclass needs planning for, too)

Personally, I *want* static typing and complaints about my dumb mistakes. It'd be nice to be able to get that without reams of boilerplate, which itself is exactly where I'll be making a bunch of boring adult mistakes of exactly the kind that computers are better at spotting than me!

I would love to be able to user all the rich typing info in my annotations to complain when somebody (most likely me) tries passing a parameter that is of the wrong type, but isinstance(param, mapping [str, some_type]) isn't available, so I have to write loops or comprehensions or whatever by hand for each case, making the code really verbose.

Honestly, the need for either attacks of boilerplate or deep dives into reimplementing fairly for boys of functionality just to make mistakes hard is my main gripe against python.

One thing I've found appears in my current project a dozen times is that I want a way to delegate a bunch of dunders to some class member en masse so I can make a class implement, say, the mapping protocol by nominating some dict member as the real mapping. I either have to write a list of one liner methods or read up on how to write a decorator to do it.

I think all these are things I (or some library writer) could do with decorators, but I've not found such a library and I haven't dug into how to make decorators do it. I guess I'll read up on the decorator thing...


to post comments


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds