B
B
Boris192018-09-12 13:50:18
Python
Boris19, 2018-09-12 13:50:18

Why is equality true?

Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> s = "test_string"
>>> id(s[1:5]) == id(s[2:6])
True

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Glukhov, 2018-09-12
@ra1

As stated in the official documentation for language version 2.7:
https://docs.python.org/2/library/functions.html?h...

[email protected]:~$ python 
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = 'test_string'
>>> id(s[1:5]) == id(s[2:6])
True
>>> id(s[1:5])
139620785926528
>>> id(s[2:6])
139620785926528
>>> print(id(s[1:5]))
139620785926384
>>> print(id(s[2:6]))
139620785926576
>>> print(id(s[1:5]), id(s[2:6]))
(139620785926528, 139620785926528)

It's just an identifier that is unique for every object in memory at a point in time. It is logical to assume that the interpreter gives the same buffer for the slice after the first function call has completed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question