<br><br><div class="gmail_quote">On Sat, May 23, 2009 at 10:46 AM, Vincent Davis <span dir="ltr"><<a href="mailto:vincent@vincentdavis.net">vincent@vincentdavis.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Thank you that makes sense to me. Much more clear then the tutorial, I think so anyway. If you are learning about classes that you kinda expect MyClass to have counter in it. I might be nice to show that x.counter = 1 creates an instance that would look like (is this correct?)<div class="im">
<br>
<br>class MyClass:<br> """A simple example class"""<br> i = 12345<br></div> counter = 1<div><br> def f(self):<br> return 'hello world'<br></div></blockquote><div><br>
That's how it would work in almost any language other than Python. In Python, anything declared in that scope is a member of the class. For something immutable like an int, it doesn't matter. If you can change it however, you get problems. This is one of the biggest sources of problems for python beginners (you get the same behavior with default method arguments btw)<br>
<br>>>> class Foo :<br>... lst = []<br>... <br>>>> a = Foo()<br>>>> b = Foo()<br>>>> b.lst<br>[]<br>>>> a.lst.append(1)<br>>>> b.lst<br>[1]<br><br><br><br>In Python, if you want something to be a part of the instance, you have to add it to the instance. That's what the "self" parameter in the method argument list is. x.f() is just syntax sugar for MyClass.f(x). Dynamically adding variables can get very confusing, so people usually declare everything (or almost everything) they're going to use in the __init__ method discussed in section 9.3.2. <br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div> <br></div></blockquote><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im"><br></div>Thanks again<font color="#888888"></font> </blockquote><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><font color="#888888"><br>
Vincent Davis</font><div><div></div><div class="h5"><br clear="all">
<br><br><div class="gmail_quote">On Sat, May 23, 2009 at 8:24 AM, Benjamin Kaplan <span dir="ltr"><<a href="mailto:benjamin.kaplan@case.edu" target="_blank">benjamin.kaplan@case.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br><br><div class="gmail_quote"><div><div></div><div>On Sat, May 23, 2009 at 9:13 AM, Vincent Davis <span dir="ltr"><<a href="mailto:vincent@vincentdavis.net" target="_blank">vincent@vincentdavis.net</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
let me add that I see that this could be right if <span style="border-collapse: collapse;">x.counter = 1 and counter need not have anything to do with MyClass but this could be more clear. </span><div><br clear="all">
Thanks<br>Vincent Davis<br>720-301-3003<br>
<br><br></div><div><div></div><div><div class="gmail_quote">On Sat, May 23, 2009 at 7:08 AM, Vincent Davis <span dir="ltr"><<a href="mailto:vincent@vincentdavis.net" target="_blank">vincent@vincentdavis.net</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<span style="border-collapse: collapse;">Section 9.3.3 says that given,</span><div><span style="border-collapse: collapse;"><br>
</span><div><span style="border-collapse: collapse;"><div>class MyClass:</div><div> """A simple example class"""</div>
<div> i = 12345</div><div> def f(self):</div><div> return 'hello world'</div><div><br></div><div>and x = MyClass()</div><div>then this</div><div><br></div><div><div>x.counter = 1</div><div>while x.counter < 10:</div>
<div> x.counter = x.counter * 2</div><div>print(x.counter)</div><div>del x.counter</div><div><br></div><div>will print 16</div><div><br></div><div>link, <a href="http://docs.python.org/3.0/tutorial/classes.html#a-first-look-at-classes" target="_blank">http://docs.python.org/3.0/tutorial/classes.html#a-first-look-at-classes</a></div>
<div><br></div><div>I am reading this section so to learn about classes but if this is right I think I need to start over.</div><div><br></div></div><div></div></span></div></div></blockquote></div></div></div></blockquote>
</div></div><div><br>The code given is correct, though the description in the tutorial could be clearer. Basically, a class in Python is represented by a dict with strings mapping to other stuff. Internally, x.counter = 1 is just a shortcut for x.__dict__['counter'] = 1. This appears in the code as dynamically adding the variable "counter" to the instance of MyClass. Unlike in static languages, an instance variable in python doesn't need to be declared inside the class for you to use it. It also doesn't need to appear in every instance of the class.<br>
<br>The last line in the code (del x.counter) removes the "counter" key from x so that the instance variable disappears. That's how the code works "without leaving a trace".<br><br><br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div>
<div><div><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div><span style="border-collapse: collapse;"><div>
<br></div></span>Thanks<br>Vincent Davis<br>720-301-3003<br>
</div></div>
</blockquote></div><br>
</div></div><br></div>--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
<br></blockquote></div><br>
<br>--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
<br></blockquote></div><br>
</div></div><br>--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
<br></blockquote></div><br>