design question: no new attributes
Ben Finney
bignose+hates-spam at benfinney.id.au
Tue Feb 27 21:05:53 EST 2007
"Alan Isaac" <aisaac at american.edu> writes:
> OK, let me approach this from a new direction. Suppose I define a
> class that behaves I think as I stipulated::
>
> class NothingNew:
> a = 0
> def __init__(self):
> self.b = 1
> self.initialized = True
> def __setattr__(self, attr, val):
> if not hasattr(self,'initialized') or hasattr(self, attr):
> self.__dict__[attr] = val
> else:
> raise ValueError
>
> After adding some documentation, what are the downsides?
The biggest downside I see is that you're going through contortions to
solve a problem you haven't demonstrated actually exists.
A smaller problem is that the ValueError should describe what's going wrong:
raise ValueError("The author of this class thinks you're up to no good")
Really, though, adding attributes to an instance is a normal thing to
do in Python, and you've not yet shown why you want that normal
functionality to be special-cased here.
--
\ "Experience is that marvelous thing that enables you to |
`\ recognize a mistake when you make it again." -- Franklin P. |
_o__) Jones |
Ben Finney
More information about the Python-list
mailing list