[Tutor] classes : post-declaration attributes

Alan Gauld alan.gauld at btinternet.com
Sat Feb 9 10:24:47 CET 2013


On 09/02/13 07:01, neubyr wrote:
>
> I am learning Python 2.7 classes and objects. It seems like attributes
> (data attributes and methods) can be added to a class/object  even after
> it's first declaration. For example,

You can do that, but mostly you shouldn't.

Usually when classes/objects are used like that its where the object is 
just being used as a data container rather than a true object (with 
behaviour and supporting data). Often a straight dictionary is a better 
option.

Python allows us lots of freedom in how we use it with many unusual 
features. Not all of those things are advisable to use in day to day 
programming. But very occasionally you find a case where they help.
In general, beginners should avoid them.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list