New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tutorial and FAQ: how to call a method on an int #64891
Comments
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. |
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'> |
That makes sense. Perhaps, then, the tutorial should include the FAQ? (I On Wed, Feb 19, 2014 at 3:59 PM, R. David Murray <report@bugs.python.org>wrote:
|
Upon consideration, I think you are right: we should add a FAQ and link it from the tutorial. |
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). |
This is a patch that includes the faq. |
New patch after first review. |
Replace 'The right way... with >>> 3 .__class__
<class 'int'>
>>> (5).__class__
<type 'int'> |
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/ |
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. |
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. --- >>> 5 .__class__
<class 'int'>
>>> (5).__class__
<type 'int'> |
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. |
Sreepriya Chalakkal, Would you be able to prepare a pull request on GitHub for your patch? Thanks! |
Hi everyone! I'm going to work on it. I have a plan to submit my pull request in the upcoming weeks. Thanks. |
I've included Should I add the other patch as well to make this move forward? |
No. The last version did not properly incorporate my suggestion, so I will make a PR that I am willing to merge. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: