addressof object with id()
Peter Otten
__peter__ at web.de
Sun Mar 24 03:57:28 EDT 2013
Steven D'Aprano wrote:
> On Sat, 23 Mar 2013 21:00:07 -0400, Roy Smith wrote:
>
>> In article <mailman.3657.1364085583.2939.python-list at python.org>,
>> Fabian von Romberg <fromberg100 at hotmail.com> wrote:
>>
>>> Hi,
>>>
>>> I have a single questions regarding id() built-in function.
>>>
>>> example 1:
>>>
>>> var1 = "some string"
>>> var2 = "some string"
>>>
>>> if use the id() function on both, it returns exactly the same address.
>>
>> Yup.
>
> Nope. Did you actually try it?
>
>
> As far as I know, there is no Python implementation that automatically
> interns strings which are not valid identifiers. "some string" is not a
> valid identifier, due to the space.
I don't know about other implementations, but in CPython two equal strings
in the *same* *compilation* will end up with the same id as a result of
constant folding. In the interpreter:
>>> a = "some string"
>>> b = "some string"
>>> a is b
False
>>> a = "some string"; b = "some string"; a is b
True
In a script:
$ cat tmp.py
a = "some string"
b = "some string"
print a is b
$ python tmp.py
True
More information about the Python-list
mailing list