+ 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

29th Aug 2020, 12:16 AM
Oscar Santos
Oscar Santos - avatar
4 odpowiedzi
+ 4
Python code: myVar = "cool" myDict = { f"{myVar}" : "something" }
29th Aug 2020, 12:23 AM
Kevin ★
+ 4
There's the walrus operator, I believe it does what youre asking https://code.sololearn.com/cMdyiQykwPCo/?ref=app
29th Aug 2020, 12:28 AM
Steven M
Steven M - avatar
+ 2
Steven M this operator is introduced in python 3.8, if I am not wrong 🤔🤔
29th Aug 2020, 6:33 AM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar
+ 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
29th Aug 2020, 6:42 AM
Théophile
Théophile - avatar