What is the id( ) function used for?

前端 未结 13 995
傲寒
傲寒 2020-11-22 10:51

I read the Python 2 docs and noticed the id() function:

Return the “identity” of an object. This is an integer (or long integer) which is

相关标签:
13条回答
  • 2020-11-22 11:04

    id() does return the address of the object being referenced (in CPython), but your confusion comes from the fact that python lists are very different from C arrays. In a python list, every element is a reference. So what you are doing is much more similar to this C code:

    int *arr[3];
    arr[0] = malloc(sizeof(int));
    *arr[0] = 1;
    arr[1] = malloc(sizeof(int));
    *arr[1] = 2;
    arr[2] = malloc(sizeof(int));
    *arr[2] = 3;
    printf("%p %p %p", arr[0], arr[1], arr[2]);
    

    In other words, you are printing the address from the reference and not an address relative to where your list is stored.

    In my case, I have found the id() function handy for creating opaque handles to return to C code when calling python from C. Doing that, you can easily use a dictionary to look up the object from its handle and it's guaranteed to be unique.

    0 讨论(0)
  • 2020-11-22 11:06

    If you're using python 3.4.1 then you get a different answer to your question.

    list = [1,2,3]
    id(list[0])
    id(list[1])
    id(list[2])
    

    returns:

    1705950792   
    1705950808  # increased by 16   
    1705950824  # increased by 16
    

    The integers -5 to 256 have a constant id, and on finding it multiple times its id does not change, unlike all other numbers before or after it that have different id's every time you find it. The numbers from -5 to 256 have id's in increasing order and differ by 16.

    The number returned by id() function is a unique id given to each item stored in memory and it is analogy wise the same as the memory location in C.

    0 讨论(0)
  • 2020-11-22 11:06

    I'm a little bit late and i will talk about Python3. To understand what id() is and how it (and Python) works, consider next example:

    >>> x=1000
    >>> y=1000
    >>> id(x)==id(y)
    False
    >>> id(x)
    4312240944
    >>> id(y)
    4312240912
    >>> id(1000)
    4312241104
    >>> x=1000
    >>> id(x)
    4312241104
    >>> y=1000
    >>> id(y)
    4312241200
    

    You need to think about everything on the right side as objects. Every time you make assignment - you create new object and that means new id. In the middle you can see a "wild" object which is created only for function - id(1000). So, it's lifetime is only for that line of code. If you check the next line - you see that when we create new variable x, it has the same id as that wild object. Pretty much it works like memory address.

    0 讨论(0)
  • 2020-11-22 11:07

    The answer is pretty much never. IDs are mainly used internally to Python.

    The average Python programmer will probably never need to use id() in their code.

    0 讨论(0)
  • 2020-11-22 11:08

    As of in python 3 id is assigned to a value not a variable. This means that if you create two functions as below, all the three id's are the same.

    >>> def xyz():
    ...     q=123
    ...     print(id(q))
    ...
    >>> def iop():
    ...     w=123
    ...     print(id(w))
    >>> xyz()
    1650376736
    >>> iop()
    1650376736
    >>> id(123)
    1650376736
    
    0 讨论(0)
  • 2020-11-22 11:14

    Your post asks several questions:

    What is the number returned from the function?

    It is "an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime." (Python Standard Library - Built-in Functions) A unique number. Nothing more, and nothing less. Think of it as a social-security number or employee id number for Python objects.

    Is it the same with memory addresses in C?

    Conceptually, yes, in that they are both guaranteed to be unique in their universe during their lifetime. And in one particular implementation of Python, it actually is the memory address of the corresponding C object.

    If yes, why doesn't the number increase instantly by the size of the data type (I assume that it would be int)?

    Because a list is not an array, and a list element is a reference, not an object.

    When do we really use id( ) function?

    Hardly ever. id() (or its equivalent) is used in the is operator.

    0 讨论(0)
提交回复
热议问题