This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Tutorial and FAQ: how to call a method on an int
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: Amir.Rastkhadiv20, Jon.Shemitz, cheryl.sabella, docs@python, josh.r, jstasiak, miss-islington, pitrou, r.david.murray, sreepriya, terry.reedy, za
Priority: normal Keywords: easy, patch

Created on 2014-02-19 22:50 by Jon.Shemitz, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
doc1.patch sreepriya, 2014-03-11 11:25 review
doc2.patch sreepriya, 2014-03-11 17:14 A new faq : 'Syntax error while calling a method on an integer value' review
doc3.patch sreepriya, 2014-03-12 13:10 A new faq : 'Syntax error while looking up an attribute on an integer literal' review
doc4.patch sreepriya, 2014-03-12 21:42 A new faq : 'Syntax error while looking up an attribute on an integer literal' review
Pull Requests
URL Status Linked Edit
PR 28818 closed za, 2021-10-08 08:07
PR 28918 merged terry.reedy, 2021-10-13 04:57
PR 28919 merged miss-islington, 2021-10-13 05:15
PR 28920 merged miss-islington, 2021-10-13 05:15
Messages (19)
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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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
History
Date User Action Args
2022-04-11 14:57:58adminsetgithub: 64891
2021-10-13 05:54:57terry.reedysetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-10-13 05:40:22miss-islingtonsetmessages: + msg403803
2021-10-13 05:38:00miss-islingtonsetmessages: + msg403801
2021-10-13 05:15:14miss-islingtonsetpull_requests: + pull_request27211
2021-10-13 05:15:10miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request27210
2021-10-13 05:15:06terry.reedysetmessages: + msg403796
2021-10-13 04:57:41terry.reedysetpull_requests: + pull_request27209
2021-10-12 15:52:55terry.reedysetassignee: docs@python -> terry.reedy
messages: + msg403742
2021-10-12 08:52:13zasetmessages: + msg403718
2021-10-08 08:07:58zasetnosy: + za
pull_requests: + pull_request27136
2021-04-28 12:33:57Amir.Rastkhadiv20setversions: + Python 3.9, - Python 3.10
nosy: + Amir.Rastkhadiv20

messages: + msg392191

components: - Interpreter Core
2021-03-22 21:40:24iritkatrielsetkeywords: + easy
versions: + Python 3.10, - Python 2.7
2017-07-03 23:14:27cheryl.sabellasetnosy: + cheryl.sabella
messages: + msg297619
2017-07-03 22:55:50jstasiaksetnosy: + jstasiak
2014-12-04 01:52:05josh.rsetnosy: + josh.r
2014-11-26 23:57:11berker.peksaglinkissue22948 superseder
2014-03-14 21:37:22sreepriyasetmessages: + msg213591
2014-03-14 19:08:47terry.reedysettitle: 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:26r.david.murraysetmessages: + msg213584
2014-03-14 17:21:09pitrousetnosy: + pitrou
messages: + msg213572
2014-03-12 21:42:07sreepriyasetfiles: + doc4.patch
2014-03-12 18:57:19terry.reedysetmessages: + msg213276
2014-03-12 13:10:29sreepriyasetfiles: + doc3.patch
2014-03-11 17:14:13sreepriyasetfiles: + doc2.patch

messages: + msg213152
2014-03-11 11:25:31sreepriyasetfiles: + doc1.patch

nosy: + sreepriya
messages: + msg213140

keywords: + patch
2014-02-21 22:53:00terry.reedysetnosy: + terry.reedy
messages: + msg211884
2014-02-21 14:43:55r.david.murraysetstatus: 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:23Jon.Shemitzsetmessages: + msg211676
2014-02-19 23:59:47r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg211674

resolution: not a bug
stage: resolved
2014-02-19 22:50:24Jon.Shemitzcreate