+ 3
Does python has something like this?
Hello everyone. I was learning at Javascript course and came across with this lesson. https://www.sololearn.com/learn/JavaScript/2975/ The second part 'computed property names' I was wondering if python has a way to create variables like this? Because every time I want to create let say user1, user2, user3... I create a list of users and append 'user' + i with for loop. But what if i would like to save something of each user? I know with poo I could save values with class variables and asign a value for each user. Let say user1.something = 'something' and so on for each user but it would be great to create variables with that kind of concatenation on the name of the variable
4 Answers
+ 4
Python code:
myVar = "cool"
myDict = {
f"{myVar}" : "something"
}
+ 4
There's the walrus operator, I believe it does what youre asking
https://code.sololearn.com/cMdyiQykwPCo/?ref=app
+ 1
Maybe this could help.
I'm using 'setattr' here : it allows you to add dynamically an attribute to an object.
https://code.sololearn.com/chcVCqnWqj4A/?ref=app