You probably want a dict instead of separate variables. For example
d = {}
for i in range(3):
d["group" + str(i)] = self.getGroup(selected, header+i)
If you insist on actually modifying local variables, you could use the locals function:
for i in range(3):
locals()["group"+str(i)] = self.getGroup(selected, header+i)
On the other hand, if what you actually want is to modify instance variables of the class you're in, then you can use the setattr function
for i in group(3):
setattr(self, "group"+str(i), self.getGroup(selected, header+i)
And of course, I'm assuming with all of these examples that you don't just want a list:
groups = [self.getGroup(i,header+i) for i in range(3)]
Use a list.
groups = [0]*3
for i in xrange(3):
groups[i] = self.getGroup(selected, header + i)
or more "Pythonically":
groups = [self.getGroup(selected, header + i) for i in xrange(3)]
For what it's worth, you could try to create variables the "wrong" way, i.e. by modifying the dictionary which holds their values:
l = locals()
for i in xrange(3):
l['group' + str(i)] = self.getGroup(selected, header + i)
but that's really bad form, and possibly not even guaranteed to work.
how do i change variable name?
Programmatically, yes, but it is not recommended.
You've described what you want to do but not why. If you care to share more, we could probably advise better ways of achieving your objective.
In the absence of other information, the obvious alternative approach would be to use a dictionary.
More on reddit.comPython script to rename variables in a Python code
Change a variable name in all places in Python - Stack Overflow
How to change the variable name all at once?
Videos
Python variables are references to objects, so you can simply create a second name pointing to the existing object and delete the old name:
y = x
del x
Ideally, the good approach will be renaming the variable, like y = x
But you can do this in weird approach with using globals()
In [1]: x = 10
In [2]: globals()['y'] = globals().pop('x')
In [3]: 'x' in globals()
Out[4]: False
In [4]: 'y' in globals()
Out[4]: True
You can accomplish this using nearly any text editor you want by using the Find and Replace feature.
The normal keyboard shortcut (including python's IDLE) for this is Ctrl+H, though different editors have different shortcuts.
In WingIDE you have the option via the Refactor Menu to do Rename Symbol.
This does an intelligent replace, and also takes care of distinguishing global variable from class attributes with the same name.
Hello everyone,
So, as a software developer at my first ever job i was required to make some windows and linux native softwares to collect data from machine sensors(like temprature , load,machine status etc) which are all made in python using PyQt5 framework also dashboards frontend to visualize collected data in javascript HTML and CSS , in the past 5 months i have been working on these softwares alone and there is no one at my senior level who knows coding and no teammates and there are no code reviews no documentations they just required me to implement all the features that they required, so after writing almost 7000-8000 lines of code learning by myself whereever i got stuck i realised the variable names in my code is really really bad , and whenever i look at my own code it looks like some 5 year old kid wrote those variable names and it really depresses me , i never followed any one naming convention in my code , so i would choose any one of these naming method and not following a single one , for example if i want to write a variable for reading delay i would write
readingDelay or reading_delay or ReadingDelay or readingdelay
and in my whole project its repeated everywhere , also my code works great in production and the upper management is happy with it as they just dont know any programming stuff and only look at the end result
i feel really bad about this how can i fix it? i have been coding for only last 6 months and this is my 1st project which i am getting paid so i want to give my best at it
sorry for my bad english
Is there a some keyboard shortcut to do so? Thanks
I wasn't really sure how to describe this, but bascially this is what I want to do:
a1=1;a2=2;a3=3
for i in [1,2,3]: print a(i)
I want this to print a1, a2, and a3. Is there a way to make that work?
"I want to define several variables with trailing ascending numbers 0, 1, 2, etc." is a red flag that you really should be using a list.
You probably want to defer to r/learnpython for these types of questions.
Nevertheless, it's not clear what you want to do. On the one hand, you could just make a a list and index into that:
a = [1, 2, 3]
for i in [0, 1, 2]:
print(a[i])
Or, probably what you want to do is put these things in a dictionary:
data = {'a1': 1, 'a2': 2, 'a3': 3}
for i in [1, 2, 3]:
print(data['a{}'.format(i)])