msg129357 - (view) |
Author: Boštjan Mejak (Retro) |
Date: 2011-02-25 12:25 |
I have found a possible typo in an example code of Python 3.2. It's located on page 32 in the PDF version of the FAQ document. The code is:
class C:
count = 0 # number of times C.__init__ called
def __init__(self):
C.count = C.count + 1
def getcount(self):
return C.count # or return self.count
The code block of the __init__ method is the thing that has the typo. Shouldn't C.count = C.count + 1 be c.count = C.count + 1 ? Because the text after this code example says:
c.count also refers to C.count for any c such that isinstance(c, C) holds /.../.
How can c.count also refer to C.count if there is no c.count in the present code example in the FAQ of Python 3.2?
If this is a typo, please fix it for Python 3.2 and also for other Python versions with the same typo.
Else if this is not a typo, explain how can c.count refer to C.count if there is no c.count in the code example.
|
msg129362 - (view) |
Author: R. David Murray (r.david.murray) * |
Date: 2011-02-25 13:23 |
Read a little further:
Caution: within a method of C, an assignment like ``self.count = 42`` creates a new and unrelated instance named "count" in ``self``'s own dict.
That is, c.count refers to C.count right up until the point where c.count is assigned a value. So, c.count = c.count + 1 will add one to the current value of C.count, and assign it to the *instance* variable c.count. c.count at that point no longer refers to the *class* variable C.count. Thus your change to the __init__ function would completely defeat the purpose of the example (which is to show how to use a *class* variable.
If you can suggest a concise wording that would have made this clearer to you, we can consider a doc patch.
|
msg129363 - (view) |
Author: R. David Murray (r.david.murray) * |
Date: 2011-02-25 13:27 |
Hmm. Rereading your message, is seems like you just didn't understand the statement "c.count refers to C.count for any...". That is a statement about how the language behaves. If there is not yet an instance variable 'count', but a class variable 'count' exists, then the value of the class variable is used when c.count is evaluated. A class is a two level nested name space with a couple of special properties.
I don't really see any way to make that statement clearer.
|
msg129366 - (view) |
Author: Boštjan Mejak (Retro) |
Date: 2011-02-25 13:57 |
>
> Caution: within a method of C, an assignment like ``self.count = 42``
> creates a new and unrelated instance named "count" in ``self``'s own dict.
More clear is to say *Caution: within a method of class C, an assignment
like ``self.count = 42`` creates a new and unrelated instance named "count"
in ``self``'s own dict.*
*
*
|
msg129390 - (view) |
Author: Boštjan Mejak (Retro) |
Date: 2011-02-25 16:54 |
So you're saying that if a class' name is C, then c.count is the same as
C.count? I thought Python is case-sensitive. You know: c (small letter c) is
not equal to C (big letter C) in Python. I don't understand what you mean by
c.count because my intepreter always throws an exception that c is not
defined if I feed it with this code example and do >>> c.count
|
msg129392 - (view) |
Author: Georg Brandl (georg.brandl) * |
Date: 2011-02-25 17:06 |
Now it's clear that David is right -- what "c" refers to is defined in this very sentence.
|
msg129393 - (view) |
Author: Boštjan Mejak (Retro) |
Date: 2011-02-25 17:23 |
I don't understand it. My bad. Please explain to me exactly what "c" refers
to.
|
msg129394 - (view) |
Author: R. David Murray (r.david.murray) * |
Date: 2011-02-25 17:25 |
Please consult the python tutor's list, the bug tracker is not the place to get introductory help with Python.
|
msg129416 - (view) |
Author: Boštjan Mejak (Retro) |
Date: 2011-02-25 20:13 |
How awful! A little pointer to the tutorial where this is explained would be
also quite smashing.
|
msg129425 - (view) |
Author: Boštjan Mejak (Retro) |
Date: 2011-02-25 20:50 |
"c.count also refers to C.count" Where in the code is "c.count"? Please
explain this for the sake of really closing this issue.
|
msg129453 - (view) |
Author: Éric Araujo (eric.araujo) * |
Date: 2011-02-25 22:53 |
If the tutorial is not clear enough, try for example <http://diveintopython.org/object_oriented_framework/class_attributes.html>. Please ask on a suitable venue if you don’t understand instead of this tracker.
(Please don’t send HTML email to this tracker, it creates false attachments.)
|
msg129472 - (view) |
Author: Boštjan Mejak (Retro) |
Date: 2011-02-25 23:42 |
Eric, the thing I don't understand is why is there that "c.count" thing. How is it possible for "c.count" to be the same reference as "C.count"? Where is "c" defined?
|
msg129487 - (view) |
Author: Éric Araujo (eric.araujo) * |
Date: 2011-02-26 01:28 |
What this part means is: “If you create an instance of C named c, and get c.count, it will get the attribute count defined on C.” IOW: You can get a class variable from any instance.
In the example, the code in __init__ cannot assign to self.count, because it wants to increment the attribute on the class, not on the instance. This is very well explained in Dive Into Python.
Please experiment in a shell and ask on appropriate venues after this message.
|
msg129513 - (view) |
Author: Boštjan Mejak (Retro) |
Date: 2011-02-26 08:51 |
Understood. Now I get it. If you create an instance "c" of the class "C" (so c = C() ) and try to get the variable "count" from that class, then "c.count" is the same reference as "C.count".
Please make that clearer in the FAQ by saying "If you create an instance "c" of the class "C", then "c.count" is the same reference as "C.count".
|
|
Date |
User |
Action |
Args |
2022-04-11 14:57:13 | admin | set | github: 55527 |
2011-02-26 08:51:01 | Retro | set | nosy:
georg.brandl, Retro, r.david.murray messages:
+ msg129513 |
2011-02-26 01:29:39 | eric.araujo | set | assignee: docs@python ->
nosy:
- eric.araujo, docs@python |
2011-02-26 01:28:55 | eric.araujo | set | nosy:
georg.brandl, eric.araujo, Retro, r.david.murray, docs@python messages:
+ msg129487 |
2011-02-25 23:42:57 | Retro | set | nosy:
georg.brandl, eric.araujo, Retro, r.david.murray, docs@python messages:
+ msg129472 |
2011-02-25 22:53:18 | eric.araujo | set | nosy:
+ eric.araujo messages:
+ msg129453
|
2011-02-25 22:51:42 | eric.araujo | set | files:
- unnamed nosy:
georg.brandl, Retro, r.david.murray, docs@python |
2011-02-25 22:51:40 | eric.araujo | set | files:
- unnamed nosy:
georg.brandl, Retro, r.david.murray, docs@python |
2011-02-25 22:51:38 | eric.araujo | set | files:
- unnamed nosy:
georg.brandl, Retro, r.david.murray, docs@python |
2011-02-25 22:51:32 | eric.araujo | set | files:
- unnamed nosy:
georg.brandl, Retro, r.david.murray, docs@python |
2011-02-25 22:51:30 | eric.araujo | set | files:
- unnamed nosy:
georg.brandl, Retro, r.david.murray, docs@python |
2011-02-25 20:50:14 | Retro | set | files:
+ unnamed
messages:
+ msg129425 nosy:
georg.brandl, Retro, r.david.murray, docs@python |
2011-02-25 20:13:14 | Retro | set | files:
+ unnamed
messages:
+ msg129416 nosy:
georg.brandl, Retro, r.david.murray, docs@python |
2011-02-25 17:25:57 | r.david.murray | set | nosy:
georg.brandl, Retro, r.david.murray, docs@python messages:
+ msg129394 |
2011-02-25 17:23:59 | Retro | set | files:
+ unnamed
messages:
+ msg129393 nosy:
georg.brandl, Retro, r.david.murray, docs@python |
2011-02-25 17:06:21 | georg.brandl | set | status: open -> closed
nosy:
+ georg.brandl messages:
+ msg129392
resolution: not a bug |
2011-02-25 16:54:28 | Retro | set | files:
+ unnamed
messages:
+ msg129390 nosy:
Retro, r.david.murray, docs@python |
2011-02-25 13:57:39 | Retro | set | files:
+ unnamed
messages:
+ msg129366 nosy:
Retro, r.david.murray, docs@python |
2011-02-25 13:27:38 | r.david.murray | set | nosy:
Retro, r.david.murray, docs@python messages:
+ msg129363 |
2011-02-25 13:23:28 | r.david.murray | set | type: behavior
messages:
+ msg129362 nosy:
+ r.david.murray |
2011-02-25 12:25:14 | Retro | create | |