design question: no new attributes

Alan Isaac aisaac at american.edu
Wed Feb 28 07:41:47 EST 2007


Ben Finney writes:
> 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.


I accept your earlier point that if an interface changes
one can just trap use of the old interface.  But I remain
interested in preventing dynamic attribute creation in
particular cases.

I have not tried to argue that dynamic attribute creation
can be dangerous for a couple reasons.
- There is no reason to expect this list to be receptive.
- I do not have the experience to make a general argument.
- I do not always find it a problem, but only in certain situations.

However I will observe that
- entire languages are structured on the premise that dynamic
attribute creation can be hazardous
- debuggers watch out for dynamic attribute creation, which
tells us it is a common source of bugs
- I sincerely doubt that anyone who has written more than
a couple scripts in Python has never accidentally created an
attribute dynamically while intending to assign to an existing
attribute.

I know the response: write good unit tests.  OK, but right now
I am writing code where this restriction will serve as a reasonable
error check, and the design I offered allows very easy removal
of the restriction in the future. Say, once I have written adequate
unit tests.

Alan





More information about the Python-list mailing list