On 12 Gen, 00:02, Paul Rubin <http://phr...@nospam.invalid> wrote: > Carl Banks <pavlovevide...@gmail.com> writes: > > and where it was manipulated for that matter. > > > This criticism is completely unfair. Instance variables have to be > > manipulated somewhere, and unless your object is immutable, that is > > going to happen outside of __init__. That's true in Java, C++, and > > pretty much any other language. > > The criticism is very valid. Some languages do support immutable > variables (e.g. "final" declarations in Java, "const" in C++, or > universal immutability in pure functional languages) and they do so > precisely for the purpose of taming the chaos of uncontrolled > mutation. It would be great if Python also supported immutability. > > > I'm not going to argue that this doesn't hurt readability, because it > > does (though not nearly as much as you claim). But there are other > > considerations, and in this case the flexibility of defining > > attributes outside __init__ is worth the potential decrease in > > readability. > > There are cases where this is useful but they're not terribly common. > I think it would be an improvement if creating new object attributes > was by default not allowed outside the __init__ method. In the cases > where you really do want to create new attributes elsewhere, you'd > have to explicitly enable this at instance creation time, for example > by inheriting from a special superclass: > > class Foo (DynamicAttributes, object): pass >
You cannot do that, but you can establish a fixed set of attributes by defining the __slot__ class variable. Ciao ----- FB -- http://mail.python.org/mailman/listinfo/python-list