[Tutor] classes : post-declaration attributes

neubyr neubyr at gmail.com
Sat Feb 9 08:01:37 CET 2013


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,

<code>
class A(object):
  def __init__(self,arg):
    self.val1 = arg

a = A(1)
print a.val1
</code>

Now another data attribute val2 can be added as:
<code>
a.val2 =  2
A.val2 = 22
</code>

I understand that all attributes are stored in a dictionary, but I am not
following when adding an attribute after initial declaration approach
should be used. The official documentation also suggests that valid
attributes are all the names present when the class object was created [1].
So does it mean post-declaration attributes are invalid? Why are they
allowed to be created then (any special use cases/examples)?

1. http://docs.python.org/2/tutorial/classes.html

--
thanks,
N
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130209/ff906eab/attachment.html>


More information about the Tutor mailing list