error in tutorial for 3.0, section 9.3.3

Vincent Davis vincent at vincentdavis.net
Sat May 23 16:46:39 CEST 2009


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?)

class MyClass:
    """A simple example class"""
    i = 12345
    counter = 1
    def f(self):
        return 'hello world'

Thanks again

Vincent Davis


On Sat, May 23, 2009 at 8:24 AM, Benjamin Kaplan
<benjamin.kaplan at case.edu>wrote:

>
>
> On Sat, May 23, 2009 at 9:13 AM, Vincent Davis <vincent at vincentdavis.net>wrote:
>
>> let me add that I see that this could be right if x.counter = 1 and
>> counter need not have anything to do with MyClass but this could be more
>> clear.
>> Thanks
>> Vincent Davis
>> 720-301-3003
>>
>>
>> On Sat, May 23, 2009 at 7:08 AM, Vincent Davis <vincent at vincentdavis.net>wrote:
>>
>>> Section 9.3.3 says that given,
>>> class MyClass:
>>>     """A simple example class"""
>>>     i = 12345
>>>     def f(self):
>>>         return 'hello world'
>>>
>>> and x = MyClass()
>>> then this
>>>
>>> x.counter = 1
>>> while x.counter < 10:
>>>     x.counter = x.counter * 2
>>> print(x.counter)
>>> del x.counter
>>>
>>> will print 16
>>>
>>> link,
>>> http://docs.python.org/3.0/tutorial/classes.html#a-first-look-at-classes
>>>
>>> I am reading this section so to learn about classes but if this is right
>>> I think I need to start over.
>>>
>>>
> 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.
>
> 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".
>
>
>
>
>>
>>> Thanks
>>> Vincent Davis
>>> 720-301-3003
>>>
>>
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090523/5d8d6217/attachment.html>


More information about the Python-list mailing list