msg211670 - (view) |
Author: Jon Shemitz (Jon.Shemitz) |
Date: 2014-02-19 22:50 |
The tutorial says "Each value is an object, and therefore has a class (also called its type). It is stored as object.__class__."
So, I tried
>>> 3.__class__
File "<stdin>", line 1
3.__class__
^
SyntaxError: invalid syntax
Yet, "foo".__class__ worked, as did 3j.__class__ and 3.5.__class__.
When my son (!) suggested that I try (3).__class__, I did indeed get <type 'int'>, while (3,).__class__ gave <type 'tuple'>.
This *looks like* a minor error in the parser, where seeing \d+\. puts it in a state where it expects \d+ and it can't handle \w+
This may be the sort of thing that only a newbie would even think to try, so may not be worth fixing. If so, it may be worth mentioning in the tutorial.
|
msg211674 - (view) |
Author: R. David Murray (r.david.murray) * |
Date: 2014-02-19 23:59 |
It's actually almost a FAQ at this point. The answer is that because of the way the parser works (it's a relatively simple parser, and we want to keep it that way), the tokenizer sees the '.' as making the token a float, and '3.__class__' is not a valid float token. So you have to precede the period by something that allows the tokenizer to know it isn't a decimal point. Parens is one way. Believe it or not, a space is another:
>>> 3 .__class__
<class 'int'>
|
msg211676 - (view) |
Author: Jon Shemitz (Jon.Shemitz) |
Date: 2014-02-20 00:04 |
That makes sense. Perhaps, then, the tutorial should include the FAQ? (I
can't be the only person who thought to try this.)
On Wed, Feb 19, 2014 at 3:59 PM, R. David Murray <report@bugs.python.org>wrote:
>
> R. David Murray added the comment:
>
> It's actually almost a FAQ at this point. The answer is that because of
> the way the parser works (it's a relatively simple parser, and we want to
> keep it that way), the tokenizer sees the '.' as making the token a float,
> and '3.__class__' is not a valid float token. So you have to precede the
> period by something that allows the tokenizer to know it isn't a decimal
> point. Parens is one way. Believe it or not, a space is another:
>
> >>> 3 .__class__
> <class 'int'>
>
> ----------
> nosy: +r.david.murray
> resolution: -> invalid
> stage: -> committed/rejected
> status: open -> closed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue20692>
> _______________________________________
>
|
msg211861 - (view) |
Author: R. David Murray (r.david.murray) * |
Date: 2014-02-21 14:43 |
Upon consideration, I think you are right: we should add a FAQ and link it from the tutorial.
|
msg211884 - (view) |
Author: Terry J. Reedy (terry.reedy) * |
Date: 2014-02-21 22:53 |
I agree that the tutorial should somewhere make it clear (possibly with a FAQ link) that int literals must be parenthesized or spaced before .name attribute access because <literal>.name is parsed as (<literal>.)name.
That is a consequence of float literals not requiring a fractional part (unlike some other languages).
|
msg213140 - (view) |
Author: Sreepriya Chalakkal (sreepriya) * |
Date: 2014-03-11 11:25 |
This is a patch that includes the faq.
|
msg213152 - (view) |
Author: Sreepriya Chalakkal (sreepriya) * |
Date: 2014-03-11 17:14 |
New patch after first review.
|
msg213276 - (view) |
Author: Terry J. Reedy (terry.reedy) * |
Date: 2014-03-12 18:57 |
Replace 'The right way... with
----
To look up an attribute on an integer literal, separate the literal from the period with either a space or parentheses.
>>> 3 .__class__
<class 'int'>
>>> (5).__class__
<type 'int'>
|
msg213572 - (view) |
Author: Antoine Pitrou (pitrou) * |
Date: 2014-03-14 17:21 |
I am not a native English speaker, but Sreepriya's latest patch looks ok to me (I am not sure the link from classes.rst is useful, though).
Sreepriya, have you already signed the contributor's agreement? Otherwise, you can sign it online at http://www.python.org/psf/contrib/contrib-form/
|
msg213584 - (view) |
Author: R. David Murray (r.david.murray) * |
Date: 2014-03-14 18:45 |
I might tweak a couple words for flow, but it looks good. I do wonder about the repetition of the bit about parenthesis or whitespace that now exists. I wonder if the first occurrence of it should now be dropped.
|
msg213585 - (view) |
Author: Terry J. Reedy (terry.reedy) * |
Date: 2014-03-14 19:08 |
I agree with Antoine about the particular cross-link and would drop that one. Is there somewhere earlier in the tutorial that discusses .attribute access? That would be the place to mention the ints and dotted names. Rather than a link, I would just mention that ints need to be separated from the period.
I also agree with David. Here is a condensed answer that I think says just what is needed.
---
This is because the Python parser sees an integer literal followed by a period as a float literal and a float literal followed by a name,
``5. __class__``, is a syntax error. To look up an attribute on an integer literal, separate the integer from the period with either a space or parentheses.
>>> 5 .__class__
<class 'int'>
>>> (5).__class__
<type 'int'>
|
msg213591 - (view) |
Author: Sreepriya Chalakkal (sreepriya) * |
Date: 2014-03-14 21:37 |
In tutorials, under section 3.1.1 - Numbers, it is mentioned about the type of integers. And also a statement as "we will see more about numeric types later in the tutorial". May be we can mention about type class there. But it might be too early to mention about classes under Numbers for a learner.
Otherwise, I also agree that the cross link is not very essential and could be dropped.
|
msg297619 - (view) |
Author: Cheryl Sabella (cheryl.sabella) * |
Date: 2017-07-03 23:14 |
Sreepriya Chalakkal,
Would you be able to prepare a pull request on GitHub for your patch?
Thanks!
|
msg392191 - (view) |
Author: Amir (Amir.Rastkhadiv20) |
Date: 2021-04-28 12:33 |
Hi everyone!
I'm going to work on it. I have a plan to submit my pull request in the upcoming weeks.
Thanks.
|
msg403718 - (view) |
Author: za (za) * |
Date: 2021-10-12 08:52 |
I've included `doc1.patch` in the github PR https://github.com/python/cpython/pull/28818
Should I add the other patch as well to make this move forward?
|
msg403742 - (view) |
Author: Terry J. Reedy (terry.reedy) * |
Date: 2021-10-12 15:52 |
No. The last version did not properly incorporate my suggestion, so I will make a PR that I am willing to merge.
|
msg403796 - (view) |
Author: Terry J. Reedy (terry.reedy) * |
Date: 2021-10-13 05:15 |
New changeset 380c44087505d0d560f97e325028f27393551164 by Terry Jan Reedy in branch 'main':
bpo-20692: Add Programming FAQ entry for 1.__class__ error. (GH-28918)
https://github.com/python/cpython/commit/380c44087505d0d560f97e325028f27393551164
|
msg403801 - (view) |
Author: miss-islington (miss-islington) |
Date: 2021-10-13 05:38 |
New changeset 47673c47db352916384e4f35fa520e48475e2601 by Miss Islington (bot) in branch '3.10':
bpo-20692: Add Programming FAQ entry for 1.__class__ error. (GH-28918)
https://github.com/python/cpython/commit/47673c47db352916384e4f35fa520e48475e2601
|
msg403803 - (view) |
Author: miss-islington (miss-islington) |
Date: 2021-10-13 05:40 |
New changeset cc90732d15b267feb4cb75ec4c448a3c66e6c520 by Miss Islington (bot) in branch '3.9':
bpo-20692: Add Programming FAQ entry for 1.__class__ error. (GH-28918)
https://github.com/python/cpython/commit/cc90732d15b267feb4cb75ec4c448a3c66e6c520
|
|
Date |
User |
Action |
Args |
2022-04-11 14:57:58 | admin | set | github: 64891 |
2021-10-13 05:54:57 | terry.reedy | set | status: open -> closed resolution: fixed stage: patch review -> resolved |
2021-10-13 05:40:22 | miss-islington | set | messages:
+ msg403803 |
2021-10-13 05:38:00 | miss-islington | set | messages:
+ msg403801 |
2021-10-13 05:15:14 | miss-islington | set | pull_requests:
+ pull_request27211 |
2021-10-13 05:15:10 | miss-islington | set | nosy:
+ miss-islington pull_requests:
+ pull_request27210
|
2021-10-13 05:15:06 | terry.reedy | set | messages:
+ msg403796 |
2021-10-13 04:57:41 | terry.reedy | set | pull_requests:
+ pull_request27209 |
2021-10-12 15:52:55 | terry.reedy | set | assignee: docs@python -> terry.reedy messages:
+ msg403742 |
2021-10-12 08:52:13 | za | set | messages:
+ msg403718 |
2021-10-08 08:07:58 | za | set | nosy:
+ za pull_requests:
+ pull_request27136
|
2021-04-28 12:33:57 | Amir.Rastkhadiv20 | set | versions:
+ Python 3.9, - Python 3.10 nosy:
+ Amir.Rastkhadiv20
messages:
+ msg392191
components:
- Interpreter Core |
2021-03-22 21:40:24 | iritkatriel | set | keywords:
+ easy versions:
+ Python 3.10, - Python 2.7 |
2017-07-03 23:14:27 | cheryl.sabella | set | nosy:
+ cheryl.sabella messages:
+ msg297619
|
2017-07-03 22:55:50 | jstasiak | set | nosy:
+ jstasiak
|
2014-12-04 01:52:05 | josh.r | set | nosy:
+ josh.r
|
2014-11-26 23:57:11 | berker.peksag | link | issue22948 superseder |
2014-03-14 21:37:22 | sreepriya | set | messages:
+ msg213591 |
2014-03-14 19:08:47 | terry.reedy | set | title: Tutorial section 9.4 and FAQ: how to call a method on an int -> Tutorial and FAQ: how to call a method on an int messages:
+ msg213585 stage: needs patch -> patch review |
2014-03-14 18:45:26 | r.david.murray | set | messages:
+ msg213584 |
2014-03-14 17:21:09 | pitrou | set | nosy:
+ pitrou messages:
+ msg213572
|
2014-03-12 21:42:07 | sreepriya | set | files:
+ doc4.patch |
2014-03-12 18:57:19 | terry.reedy | set | messages:
+ msg213276 |
2014-03-12 13:10:29 | sreepriya | set | files:
+ doc3.patch |
2014-03-11 17:14:13 | sreepriya | set | files:
+ doc2.patch
messages:
+ msg213152 |
2014-03-11 11:25:31 | sreepriya | set | files:
+ doc1.patch
nosy:
+ sreepriya messages:
+ msg213140
keywords:
+ patch |
2014-02-21 22:53:00 | terry.reedy | set | nosy:
+ terry.reedy messages:
+ msg211884
|
2014-02-21 14:43:55 | r.david.murray | set | status: closed -> open title: Tutorial section 9.4 -> Tutorial section 9.4 and FAQ: how to call a method on an int messages:
+ msg211861
resolution: not a bug -> (no value) stage: resolved -> needs patch |
2014-02-20 00:04:23 | Jon.Shemitz | set | messages:
+ msg211676 |
2014-02-19 23:59:47 | r.david.murray | set | status: open -> closed
nosy:
+ r.david.murray messages:
+ msg211674
resolution: not a bug stage: resolved |
2014-02-19 22:50:24 | Jon.Shemitz | create | |