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

is behave different for integers in 3.6 and 3.7 #80773

Closed
GeraldoXexeo mannequin opened this issue Apr 10, 2019 · 5 comments
Closed

is behave different for integers in 3.6 and 3.7 #80773

GeraldoXexeo mannequin opened this issue Apr 10, 2019 · 5 comments
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@GeraldoXexeo
Copy link
Mannequin

GeraldoXexeo mannequin commented Apr 10, 2019

BPO 36592
Files
  • testisbehavior.py: A very simple example of the behavior
  • 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 = None
    closed_at = <Date 2019-04-11.00:01:17.797>
    created_at = <Date 2019-04-10.23:55:24.967>
    labels = ['interpreter-core', '3.7', 'invalid']
    title = 'is behave different for integers in 3.6 and 3.7'
    updated_at = <Date 2019-04-11.01:52:59.579>
    user = 'https://bugs.python.org/GeraldoXexeo'

    bugs.python.org fields:

    activity = <Date 2019-04-11.01:52:59.579>
    actor = 'Shane Smith'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-04-11.00:01:17.797>
    closer = 'eric.smith'
    components = ['Interpreter Core']
    creation = <Date 2019-04-10.23:55:24.967>
    creator = 'Geraldo.Xexeo'
    dependencies = []
    files = ['48259']
    hgrepos = []
    issue_num = 36592
    keywords = []
    message_count = 5.0
    messages = ['339900', '339902', '339905', '339913', '339914']
    nosy_count = 2.0
    nosy_names = ['Geraldo.Xexeo', 'Shane Smith']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue36592'
    versions = ['Python 3.6', 'Python 3.7']

    @GeraldoXexeo
    Copy link
    Mannequin Author

    GeraldoXexeo mannequin commented Apr 10, 2019

    # When you run the program:
    a,b=300,300
    print(a is b)
    #you get different results in 3.6 (True) and 3.7 (False)

    @GeraldoXexeo GeraldoXexeo mannequin added 3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Apr 10, 2019
    @ericvsmith
    Copy link
    Member

    Python makes no guarantee as to whether an identity test on integers would return True or False. You should not depend on the behavior in any particular version.

    @ericvsmith ericvsmith added the type-bug An unexpected behavior, bug, or error label Apr 11, 2019
    @ShaneSmith
    Copy link
    Mannequin

    ShaneSmith mannequin commented Apr 11, 2019

    This is the sort of thing that makes me avoid "is" in favor of "==" for most applications. Understanding when two objects point to the same memory requires a deeper understanding of the underlying code than I usually want to delve into.

    Anyway, I find it interesting that for 3.7.3:
    >>> a, b = 256, 256
    >>> a is b
    True
    
    >>> a, b = 257, 257
    >>> a is b
    False

    So 2**8 is a magic number, for whatever reason. I'll be sticking with "=="...

    @ShaneSmith ShaneSmith mannequin removed the type-bug An unexpected behavior, bug, or error label Apr 11, 2019
    @GeraldoXexeo
    Copy link
    Mannequin Author

    GeraldoXexeo mannequin commented Apr 11, 2019

    "So 2**8 is a magic number, for whatever reason."

    Actually, this is true. Accordingly to https://rushter.com/blog/python-integer-implementation/

    "Optimization of commonly-used integers
    Small integer objects in a range of -5 to 256 are always pre-allocated during initialization. Because Python integers are immutable, we can use them as singletons. Every time you need to create small integer instead of creating new object Python just points to already allocated one. Thereby it saves a lot of space and computation for commonly-used integers.

    Interestingly enough, the PyLongObject structure takes at least 28 bytes for every allocated integer and therefore takes three times as much memory as a simple 64-bit C integer."

    This are constants

    #define NSMALLPOSINTS           257
    #define NSMALLNEGINTS           5

    You can find the code in:

    https://github.com/python/cpython/blob/master/Objects/longobject.c

    @ShaneSmith
    Copy link
    Mannequin

    ShaneSmith mannequin commented Apr 11, 2019

    Well, then I guess that explains it! Still, like I said, I tend to shy away from features that require such a deep understanding of the implementation in order to avoid "gotchas". "is" does have its uses, but for me they very very rarely come up.

    @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.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant