How does the Python Memory Manager find existing objects? [duplicate] - python

This question already has answers here:
What determines which strings are interned and when? [duplicate]
(3 answers)
About the changing id of an immutable string
(5 answers)
Closed 9 months ago.
I am fairly ok with Python, but recently I learned something that made me question everything I thought to be true in my life :D
So if I run this:
w1 = "word"
w2 = "word"
print(id(w1))
print(id(w2))
To my utter horror, the output is:
140675515277936
140675515277936
I always believed that as strings are immutable, they will have their own address. I was wrong. My question then:
How does the Python Memory Manager find existing string (or any) objects when a new variable is created, so it can make that new variable point to them?
With some hashing? If besides your answer you can point me to learning material about this, i will be double grateful! Cheers!

Related

What is the Python ` symbol [duplicate]

This question already has answers here:
What do backticks mean to the Python interpreter? Example: `num`
(3 answers)
Meaning of the backtick character in Python
(2 answers)
Closed 1 year ago.
Lots of old python code I look in has this ` symbol around a lot of stuff, what does it do? Now it is not considered valid syntax, obviously.
And I don't think it is just another string identifier, its sometimes wrapped around functions in the code I'm looking at.
Any help will be appreciated.

Saving a file name as a input variable [duplicate]

This question already has answers here:
String formatting in Python [duplicate]
(14 answers)
Putting a variable into a string (quote)
(4 answers)
Closed 2 years ago.
very new to python so sorry for the silly question
I've created a user input interface
fn=input()
#Where the user will input version32 for instance so effectively
fn=Version32
I've then imported a template word document using docx, which has been heavily modified based upon user input. I then want the file name output to be saved as "fn" or in this case Version32
output.save(r"C:\Users\XXX\XXX\XXX\'fn'.docx")
Where fn is a variable? Is this even possible, or am I barking up the wrong tree?
Kind Regards!!
IIUC, you can do this using f-string:
output.save(rf"C:\Users\XXX\XXX\XXX\{fn}.docx")

How to create N list with a funtion(Not by writing it) that holds a dynamic name such as list1, list2, list3,... listn? [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
How can I create multiple variables from a list of strings? [duplicate]
(2 answers)
Closed 5 years ago.
I'm a rookie at Python so I don't know much about the limits of the language, but it seems that you can't write a function or a program that creates a list on its own.
I'm working with binary lists. I'm working with 7 bits so, as you can guess, I need 128 different lists to store all possibilities.
Let me know if its possible, or if not please let me know why and what else could work for my needs.
def list_creator:
n=128
"create a new_list n times with 7 of length"
"Should display something like this"
list1=[None]*7
list2=[None]*7
list(n)=[None]*7
You should make a list containing 128 empty lists and add to those.

C command similar to "if x in list" of Python [duplicate]

This question already has answers here:
Check if a value exists in an array in Cython
(2 answers)
Closed 6 years ago.
In Python we could use if x in list: so I was wondering if there's a similar command in C, so that we don't have to go through the whole thing using a for.
How can you know whether a value is contained in an array without cycling through it? This is exactly what Python does under the hood. No, there's no magical way to instantly know this.

what's the meaning of "[ ]" in python references? [duplicate]

This question already has answers here:
What do square brackets, "[]", mean in function/class documentation?
(4 answers)
Closed 7 years ago.
I'm using python for a long time. but it was always strange to me why In python references, commands are written like this:
del var1[,var2[,var3[....,varN]]]]
I know that if I want to use the above command I should write it in this way:
del var1, var1
I can't understand the meaning of [] is it related to the lists? any help will be appreciated.
Its just showing that these parameters are optional. This is normal style for references.
There is nothing to do with this in python. Just a tutorial explanation.
I bealive, this is taken origin from Usage message

Categories

Resources