Re: [Tutor] Class definition confusion
On 15/02/2012 18:35, Hugo Arts wrote: [snip] 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 To the OP. Note that __init__ is an initialiser and not a constructor which is __new__, see e.g. http://mail.python.org/pipermail/tutor/2008-April/061426.html -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class definition confusion
On 15/02/2012 18:14, Sivaram Neelakantan 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. sivaram -- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Your impression is incorrect. This type of behaviour is allowed because of Python's dynamic nature, so the following is fine. >>> class Point: ... """pts in 2d space""" ... >>> b = Point() >>> b.x = 3 >>> b.y = 4 >>> del b.x >>> del b.y >>> b.l = 5 >>> b.m = 6 >>> print b, b.l, b.m <__main__.Point instance at 0x02FB89B8> 5 6 Also be careful of your terminology. Here we are discussing instance attributes. Class attributes are different in that they are are shared at the class level so. >>> class Point: ... """pts in 2d space""" ... x = 3 ... y = 4 ... >>> a = Point() >>> b = Point() >>> a.x 3 >>> b.y 4 HTH. -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class definition confusion
On Thu, Feb 16 2012,Alan Gauld wrote: [snipped 19 lines] > Python allows instance attributes to be added at runtime. > In general this is a bad idea IMHO, a dictionary would probably > be more appropriate, but there can, very occasionally, be valid > uses for it. Thanks for that, I kept thinking that the author had made some typos in the book and was getting progressively confused, till I tried it at the prompt. sivaram -- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class definition confusion
On Wed, Feb 15, 2012 at 7:14 PM, Sivaram Neelakantan 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
Re: [Tutor] Class definition confusion
On 15/02/12 18:14, Sivaram Neelakantan wrote: I was under the impression that you have to define the attributes of the class before using it in an instance. Only in some languages. Python is not one of those. class Point: ... """pts in 2d space""" ... 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 allows instance attributes to be added at runtime. In general this is a bad idea IMHO, a dictionary would probably be more appropriate, but there can, very occasionally, be valid uses for it. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Class definition confusion
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. sivaram -- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor