On Wed, Feb 15, 2012 at 7:14 PM, Sivaram Neelakantan <nsivaram....@gmail.com> wrote: > > I was under the impression that you have to define the attributes of > the class before using it in an instance. Following the book > 'thinking in Python', > >>>> class Point: > ... """pts in 2d space""" > ... >>>> print Point > __main__.Point >>>> b = Point() >>>> b.x =3 >>>> b.y =4 >>>> print b.y > 4 >>>> > > Why is it not throwing an error? This is confusing me a bit. >
Python is different from static languages like C++. You can add and remove attributes from objects at any time. You do not have to declare, in your class, what kind of attributes it has. An __init__ might seem like it's special in some way, declaring attributes. But it's not, really, it's just another method that gets passed the object it is called on (that would be "self"). It's only special because it gets called when an object is created, so generally an object is initialized there and attributes are assigned (hence the name "init").' HTH, Hugo _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor