design question: no new attributes

Bruno Desthuilliers bdesth.quelquechose at
Thu Mar 1 01:00:46 CET 2007

Alan Isaac a écrit :
> 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.

Right. Don't you wonder why ?

> - I do not have the experience to make a general argument.
> - I do not always find it a problem, but only in certain situations.

Which ones ?

> However I will observe that
> - entire languages are structured on the premise that dynamic
> attribute creation can be hazardous

Entire languages are structured on the premise that developers are dumb 
and that anything dynamic can be hazardous.

Seriously, if you fear dynamic attribute creation, you'd better choose 
another language. What about dynamically changing the class of an 
object? Now ain't *that* hazardous ?-)

> - debuggers watch out for dynamic attribute creation, 

Which ones ?

> 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.

Of course. But MHE (7+ years of Python programming - more than a couple 
scripts...) is that this has not proven so far to be a common and 
annoying enough problem to justify fighting against the language.

> 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,

I don't share your definition of "reasonable". But you should have 
guessed by now !-)

More information about the Python-list mailing list