Skip to content
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

Closed
JonShemitz mannequin opened this issue Feb 19, 2014 · 19 comments
Closed

Tutorial and FAQ: how to call a method on an int #64891

JonShemitz mannequin opened this issue Feb 19, 2014 · 19 comments
Assignees
Labels
3.9 only security fixes docs Documentation in the Doc dir easy type-bug An unexpected behavior, bug, or error

Comments

@JonShemitz
Copy link
Mannequin

JonShemitz mannequin commented Feb 19, 2014

BPO 20692
Nosy @terryjreedy, @pitrou, @bitdancer, @jstasiak, @MojoVampire, @csabella, @miss-islington, @Amir-Rastkhadiv, @za
PRs
  • bpo-20692 Submitted doc1.patch as github PR #28818
  • bpo-20692: Add Programming FAQ entry on 1.__class__ error. #28918
  • [3.10] bpo-20692: Add Programming FAQ entry for 1.__class__ error. (GH-28918) #28919
  • [3.9] bpo-20692: Add Programming FAQ entry for 1.__class__ error. (GH-28918) #28920
  • Files
  • doc1.patch
  • doc2.patch: A new faq : 'Syntax error while calling a method on an integer value'
  • doc3.patch: A new faq : 'Syntax error while looking up an attribute on an integer literal'
  • doc4.patch: A new faq : 'Syntax error while looking up an attribute on an integer literal'
  • 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:

    assignee = 'https://github.com/terryjreedy'
    closed_at = <Date 2021-10-13.05:54:57.501>
    created_at = <Date 2014-02-19.22:50:24.073>
    labels = ['easy', 'type-bug', '3.9', 'docs']
    title = 'Tutorial and  FAQ: how to call a method on an int'
    updated_at = <Date 2021-10-13.05:54:57.501>
    user = 'https://bugs.python.org/JonShemitz'

    bugs.python.org fields:

    activity = <Date 2021-10-13.05:54:57.501>
    actor = 'terry.reedy'
    assignee = 'terry.reedy'
    closed = True
    closed_date = <Date 2021-10-13.05:54:57.501>
    closer = 'terry.reedy'
    components = ['Documentation']
    creation = <Date 2014-02-19.22:50:24.073>
    creator = 'Jon.Shemitz'
    dependencies = []
    files = ['34352', '34355', '34375', '34382']
    hgrepos = []
    issue_num = 20692
    keywords = ['patch', 'easy']
    message_count = 19.0
    messages = ['211670', '211674', '211676', '211861', '211884', '213140', '213152', '213276', '213572', '213584', '213585', '213591', '297619', '392191', '403718', '403742', '403796', '403801', '403803']
    nosy_count = 12.0
    nosy_names = ['terry.reedy', 'pitrou', 'r.david.murray', 'docs@python', 'jstasiak', 'Jon.Shemitz', 'josh.r', 'sreepriya', 'cheryl.sabella', 'miss-islington', 'Amir.Rastkhadiv20', 'za']
    pr_nums = ['28818', '28918', '28919', '28920']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue20692'
    versions = ['Python 3.9']

    @JonShemitz
    Copy link
    Mannequin Author

    JonShemitz mannequin commented Feb 19, 2014

    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.

    @JonShemitz JonShemitz mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Feb 19, 2014
    @JonShemitz JonShemitz mannequin assigned docspython Feb 19, 2014
    @JonShemitz JonShemitz mannequin added docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error labels Feb 19, 2014
    @bitdancer
    Copy link
    Member

    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'>

    @JonShemitz
    Copy link
    Mannequin Author

    JonShemitz mannequin commented Feb 20, 2014

    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\>


    @bitdancer
    Copy link
    Member

    Upon consideration, I think you are right: we should add a FAQ and link it from the tutorial.

    @bitdancer bitdancer changed the title Tutorial section 9.4 Tutorial section 9.4 and FAQ: how to call a method on an int Feb 21, 2014
    @bitdancer bitdancer reopened this Feb 21, 2014
    @bitdancer bitdancer removed the invalid label Feb 21, 2014
    @terryjreedy
    Copy link
    Member

    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).

    @sreepriya
    Copy link
    Mannequin

    sreepriya mannequin commented Mar 11, 2014

    This is a patch that includes the faq.

    @sreepriya
    Copy link
    Mannequin

    sreepriya mannequin commented Mar 11, 2014

    New patch after first review.

    @terryjreedy
    Copy link
    Member

    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'>

    @pitrou
    Copy link
    Member

    pitrou commented Mar 14, 2014

    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/

    @bitdancer
    Copy link
    Member

    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.

    @terryjreedy
    Copy link
    Member

    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'>

    @terryjreedy terryjreedy changed the 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 Mar 14, 2014
    @sreepriya
    Copy link
    Mannequin

    sreepriya mannequin commented Mar 14, 2014

    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.

    @csabella
    Copy link
    Contributor

    csabella commented Jul 3, 2017

    Sreepriya Chalakkal,

    Would you be able to prepare a pull request on GitHub for your patch?

    Thanks!

    @iritkatriel iritkatriel added easy 3.10 only security fixes labels Mar 22, 2021
    @Amir-Rastkhadiv
    Copy link
    Mannequin

    Amir-Rastkhadiv mannequin commented Apr 28, 2021

    Hi everyone!

    I'm going to work on it. I have a plan to submit my pull request in the upcoming weeks.

    Thanks.

    @Amir-Rastkhadiv Amir-Rastkhadiv mannequin added 3.9 only security fixes and removed interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.10 only security fixes labels Apr 28, 2021
    @za
    Copy link
    Mannequin

    za mannequin commented Oct 12, 2021

    I've included doc1.patch in the github PR #28818

    Should I add the other patch as well to make this move forward?

    @terryjreedy
    Copy link
    Member

    No. The last version did not properly incorporate my suggestion, so I will make a PR that I am willing to merge.

    @terryjreedy
    Copy link
    Member

    New changeset 380c440 by Terry Jan Reedy in branch 'main':
    bpo-20692: Add Programming FAQ entry for 1.__class__ error. (GH-28918)
    380c440

    @miss-islington
    Copy link
    Contributor

    New changeset 47673c4 by Miss Islington (bot) in branch '3.10':
    bpo-20692: Add Programming FAQ entry for 1.__class__ error. (GH-28918)
    47673c4

    @miss-islington
    Copy link
    Contributor

    New changeset cc90732 by Miss Islington (bot) in branch '3.9':
    bpo-20692: Add Programming FAQ entry for 1.__class__ error. (GH-28918)
    cc90732

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes docs Documentation in the Doc dir easy type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    6 participants