Python string interning
While this question doesn’t have any real use in practice, I am curious as to how Python does string interning. I have noticed the following.
While this question doesn’t have any real use in practice, I am curious as to how Python does string interning. I have noticed the following.
Something about the id of objects of type str (in python 2.7) puzzles me. The str type is immutable, so I would expect that once it is created, it will always have the same id. I believe I don’t phrase myself so well, so instead I’ll post an example of input and output sequence.
In [55]: a = 5 In [56]: b = 6 In [57]: (a, b) = (b, a) In [58]: a Out[58]: 6 In [59]: b Out[59]: 5 How does this swapping of values of a and b work internally? Its definitely not using a temp variable. Answers: Thank you for visiting the Q&A section on … Read more
I can’t really think of any reason why Python needs the del keyword (and most languages seem to not have a similar keyword). For instance, rather than deleting a variable, one could just assign None to it. And when deleting from a dictionary, a del method could be added.
Why does CPython (no clue about other Python implementations) have the following behavior?
I understand that sets in Python are unordered, but I’m curious about the ‘order’ they’re displayed in, as it seems to be consistent. They seem to be out-of-order in the same way every time:
What algorithm is the built in sort() method in Python using? Is it possible to have a look at the code for that method?
Is there any performance difference between tuples and lists when it comes to instantiation and retrieval of elements?
If a tuple is immutable then why can it contain mutable items?
In contrast to functions, a class’ body is executed at definition time: