This question already has answers here:
Why variables holding same value has the common ID in Python? [duplicate]
(1 answer)
About the changing id of an immutable string
(5 answers)
Closed 5 years ago.
Here's my snippet of code
>>> a = "some_string"
140420665652016
>>> id(a)
>>> id("some" + "_" + "string")
140420665652016
Notice that both the ids are same. But this does not happen with integers (which are also immutable like strings).
>>> a = 999
>>> id(a)
140420666022800
>>> id(999)
140420666021200
>>> id(998 + 1)
140420666023504
I'm not able to find a reason of why it's happening only with strings.
Related
This question already has answers here:
What are the rules for cpython's string interning?
(2 answers)
Python string interning
(2 answers)
Why and where python interned strings when executing `a = 'python'` while the source code does not show that?
(1 answer)
Closed last year.
When you assign same string literal to two variables, Python only allocates one string. This is very reasonable since string is immutable object in Python.
>>> a = "Hello"
>>> b = "Hello"
>>> id(a)
4311984752
>>> id(b)
4311984752
>>> a is b
True
But the strange part is: when the string contains special character (like !), Python will allocate two strings with exact same content.
>>> a = "hi!"
>>> b = "hi!"
>>> id(a)
4328663024
>>> id(b)
4317237616
>>> a is b
False
I read about this strange behaviour from here: https://python-course.eu/python-tutorial/data-types-and-variables.php
But that guide didn't elaborate why Python does this seemingly unnecessary duplicated string allocation.
My question is what's rationale behind Python's design of duplicated string allocation for string containing special character?
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Converting a string representation of a list into an actual list object [duplicate]
(3 answers)
Closed 3 years ago.
I know the question sounds a bit vague, but this should clear it up.
a = "[200, 30.5, 37]" #This is a string literal, where a[0] = '['
b = [200, 30.5, 37] #This is a list literal, where b[0] = 200
How do I get b from a?
b = eval(a)
This should do the trick.
This question already has answers here:
About the changing id of an immutable string
(5 answers)
The `is` operator behaves unexpectedly with non-cached integers
(2 answers)
Closed 6 years ago.
a = 1234
b = 1234
print(id(a), id(b))
In the above script both IDs are the same. a and b refer to the same object. When running the commands one by one in a Python console the IDs are different:
>>> a = 1234
>>> b = 1234
>>> print(id(a), id(b))
2445680 6579168
but:
>>> print(id(1234), id(1234))
6579472 6579472
Why is 1234 a different object in successive commands, but not in one command or in a script?
I tested this on CPython 3.5.2.
This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
String comparison in Python: is vs. == [duplicate]
(4 answers)
Closed 10 years ago.
I run the following in the Python interpreter:
>>> foo = 10
>>> dir(foo) == dir(10)
True
>>> dir(foo) is dir(10)
False
>>>
Why is this?
is checks that 2 arguments refer to the same object, == checks that 2 arguments have the same value. dir() returns a list which contains the same data for both foo and 10, but the actual list instances for the 2 things are different.
This question already has answers here:
How to display a byte array as hex values
(5 answers)
Closed 4 years ago.
In my code I receive a bytearray with the value 0x41 as b'\x41'. Printing this value gives b'A'. How can I print it in such a way that i get the result b'\x41'. (Python 3.7)
Testcode:
print("b'\x41'")
Unfortunately, if you don't like the default display, you have to generate it yourself:
>>> b = b'\x41\x42\x43'
>>> print(b)
b'ABC'
>>> print("b'" + ''.join(f'\\x{c:02x}' for c in b) + "'")
b'\x41\x42\x43'