-1

I would like to make a Button that assign a new variable with a value in a compact form.

I tried this:

def whichButton(self, _var, _pressedButton):
    self._var = _pressedButton 

def checkScooter(self):
    self.checkScooter = Button(window, text="Standard", command=lambda: self.whichButton(edition, 1))
    self.checkScooter(row=1, column=0)

def checkAbonnement(self):
    self.checkAbonnement = Button(window, text="Gold", command=lambda: self.whichButton(abonnement, 3))
    self.checkAbonnement(row=1, column=0)

It just gives me an error, that "edition" is not defined, but I want the Button to define it

Any tips?

Marius1773
  • 23
  • 5
  • It isn't good practise to do that. Why not split it in 2 functions? – TheLizzard Jun 13 '21 at 22:35
  • I edited it, you mean it like that? – Marius1773 Jun 13 '21 at 22:38
  • What I means is to split it so the 2 buttons call 2 different functions. Those functions would be trivial to program if you do that. – TheLizzard Jun 13 '21 at 22:40
  • Wouldn't it overfill the program? If you would make a new function everytime? Edit: I would only have to do 2 Functions for my program but if you got multiple of those? – Marius1773 Jun 13 '21 at 22:42
  • It will make it more readable. If your program isn't readable there is no way of debugging it later. If you really want to do it your way, I can write an answer but it's really ugly and if a bug appears, ... – TheLizzard Jun 13 '21 at 22:45
  • Then I will do it with the functions, thanks bud :) – Marius1773 Jun 13 '21 at 22:45
  • No problem. By the way, just try running `import this` in python. It is built in and you should read it. It specifically says: *Readability counts.* – TheLizzard Jun 13 '21 at 22:47
  • Assigning to `self._var` sets an attribute literally named `_var` The parameter to your method of the same name has absolutely nothing to do with it. – jasonharper Jun 14 '21 at 00:11

1 Answers1

0

Your request to set a new variable is odd, and probably not the right solution to whatever problem you're trying to solve.

That being said, you can use setattr to set the value based on the name of a variable. That variable doesn't have to exist. For example, to set the variable self.edition to 1 you can do setattr(self, "edition", 1).

Therefore, you can pass in the string name of the variable to your whichButton function, and use setattr to set a variable with that name.

It would look something like this:

def whichButton(self, _var, _pressedButton):
    setattr(self, _var, _pressedButton)
...
self.checkScooter = Button(..., command=lambda: self.whichButton("edition", 1))
...
self.checkAbonnement = Button(..., command=lambda: self.whichButton("abonnement", 3))

In the above code, clicking either button will either set self.edition or self.abonnement.


There is almost certainly a better solution to your problem, but your question doesn't provide any details about what problem you're really trying to solve. A simple mprovement over this would be to use a dictionary to hold your "new" variables rather than creating literally new variables.

You can do that by defining a dictionary in your __init__ and then setting it in your whichButton function.

It would look something like this:

class Something:
    def __init__(self):
        self._vars = {}
    def whichButton(self, name, new_value):
        self._vars[name] = new_value

This has the advantage that all of these special variables exist in a single data structure, separate from the object. That means that it would be impossible to accidentally overwrite instance variables.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685