Search Results

Search found 3 results on 1 pages for 'mahi'.

Page 1/1 | 1 

  • Use eval to set unknown property in Python

    - by Mahi
    I have an User class, that has multiple properties inside it, and I also have addPoint method for User class, which allows user to input points into any of it's properties. However, there can be up to like 500 properties, everyone can have different ones, etc. So it would be a pain in the ass to code every property with "if: - elif:". Now this is what I tried to do, to make it much easier and cleaner: class User: def __init__(self): self.health = 0 self.speed = 0 def addPoint(self, property, amount): eval("self."+property) = eval("self."+property) + amount And now when I'd do fe. u = User() u.addPoint("health", 5) I would like it to do this: self.health = self.health + 5, and that's what I used the eval()s for. However, Python's just giving me error: can't assign to function call. I'm not trying to assign the eval() function call itself, I'm trying to assign the returned value from eval(), so how could I do this the easiest way?

    Read the article

1