error in tutorial for 3.0, section 9.3.3

2009-05-23 Thread Vincent Davis
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.


Thanks
Vincent Davis
720-301-3003
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: error in tutorial for 3.0, section 9.3.3

2009-05-23 Thread Vincent Davis
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 vinc...@vincentdavis.netwrote:

 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.


 Thanks
 Vincent Davis
 720-301-3003

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: error in tutorial for 3.0, section 9.3.3

2009-05-23 Thread Vincent Davis
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.kap...@case.eduwrote:



 On Sat, May 23, 2009 at 9:13 AM, Vincent Davis 
 vinc...@vincentdavis.netwrote:

 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 
 vinc...@vincentdavis.netwrote:

 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


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: error in tutorial for 3.0, section 9.3.3

2009-05-23 Thread Michiel Overtoom


Vincent writes:

 you kinda expect MyClass to have counter in it.

Yeah, that makes sense. These instance variables are often initialized 
in the __init__ method:



class Counter(object):
def __init__(self,initialvalue):
self.value=initialvalue
def inc(self):
self.value+=1
def dec(self):
self.value-=1

beans=Counter(123)
beans.inc()
beans.inc()
beans.dec()
print beans.value

# the output of the program is: 124

Greetings,


--
The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing. - Vinod Valloppillil
http://www.catb.org/~esr/halloween/halloween4.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: error in tutorial for 3.0, section 9.3.3

2009-05-23 Thread Benjamin Kaplan
On Sat, May 23, 2009 at 10:46 AM, Vincent Davis vinc...@vincentdavis.netwrote:

 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'


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)

 class Foo :
...lst = []
...
 a = Foo()
 b = Foo()
 b.lst
[]
 a.lst.append(1)
 b.lst
[1]



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.





 Thanks again


 Vincent Davis



 On Sat, May 23, 2009 at 8:24 AM, Benjamin Kaplan benjamin.kap...@case.edu
  wrote:



 On Sat, May 23, 2009 at 9:13 AM, Vincent Davis 
 vinc...@vincentdavis.netwrote:

 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 vinc...@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



 --
 http://mail.python.org/mailman/listinfo/python-list


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: error in tutorial for 3.0, section 9.3.3

2009-05-23 Thread Dave Angel

Vincent Davis 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.


Thanks
Vincent Davis
720-301-3003

  
Yes, this code will display 16, and it's not specific to 3.0, but works 
in earlier Pythons as well.


I'm not sure why you're puzzled;  it could be the question of why 16, 
but i suspect it's because you can't see who creates this x attribute.


Unlike languages like Java and C++, object attributes are not fixed at 
the time the class is compiled.  Some attributes are created by the 
placement of the code, for example the method name f.  But others are 
created in the code of the class (typically the __init__() method), and 
others can be created by anyone, at any time.


I don't know why the tutorial starts with this, but it illustrates that 
an object is really just a container for attributes, some of which are 
callable (methods), and some of which are data (what Java would call 
fields).  Anybody with an object reference can create, modify or delete 
those attributes.  If it's being done inside the class methods, you use 
the syntax  self.counter.  But it also works outside, as you see.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Re: error in tutorial for 3.0, section 9.3.3

2009-05-23 Thread Dave Angel

Vincent Davis wrote:

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.kap...@case.eduwrote:

  

On Sat, May 23, 2009 at 9:13 AM, Vincent Davis vinc...@vincentdavis.netwrote:



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 vinc...@vincentdavis.netwrote:

  

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



(You accidentally top-posted.  That makes the context much harder for 
others to follow.)


No, the attribute 'counter' you demonstrated is an attribute of the 
class.  To duplicate what  x.counter=1 does, you have to add it to the 
instance.  Normally, this would be done in the __init__() method, but 
that would put it in each instance of the class, as it's being created.  
But this one is only in the 'x' instance.



--
http://mail.python.org/mailman/listinfo/python-list