On Thu, Jul 21, 2016 at 8:11 AM, Lawrence D’Oliveiro <lawrenced...@gmail.com> wrote: > On Wednesday, July 20, 2016 at 9:16:30 PM UTC+12, Peter Otten wrote: >> >> Lawrence D’Oliveiro wrote: >> >>> On Wednesday, July 20, 2016 at 7:26:36 PM UTC+12, Peter Otten wrote: >>> >>>> pylint can detect candidates for accidental attribute creation: >>> >>> And __slots__ will prevent them outright. >> >> And attributes added intentionally. > > You mean, being able to dynamically add new attributes to an object? > > Probably not a good idea to mix that with read/write properties...
This is exactly what Steven was talking about. If you can't handle dynamic read-write attributes (whether they're properties or simple attributes is orthogonal to this), why even use Python? You can get a linter to tell you about misspelled attribute names, which has a huge advantage over __slots__ in that it can give you the information prior to actually hitting that line of code (prior to even running the program - and some editors have integrated linters, so you don't even need to consciously run the linter, you just see the results as you type). That gives you virtually the same benefit as C/Java style of coding, where you're forced to declare all your members in the class; but as an added bonus, you can *dynamically add attributes*! Don't think that's useful? Well, the other day I wanted to add a wrapper around the constructor for someone else's object to stash a bit of extra information into it (to track down a resource leak - turned out there was an ever-growing collection of objects, because they weren't being removed from that object), and if I'd been using Java, the response would have been "no way does that even make sense". Thanks to dynamic code, I could do what I needed to and track down the leak. If the author of that object had used __slots__, though, I'd have been utterly stuck. Using __slots__ basically takes your object down to the level of a Java one. That's generally fine for something immutable (you really don't need to be adding attributes to the integer 42), but even with immutables, I've sometimes wanted to add hidden attributes; with mutable objects, it's common enough that you want to leave the option open unless you have good reason not to. And "my editor sucks" is not a good reason; nor is "my debugging skills suck". ChrisA -- https://mail.python.org/mailman/listinfo/python-list