I am learning more about OOP in Python and I have hit a bit of a road block. Below is my code:
class Player: bats = 0 hit = 0 freePass = 0 out = 0 defenseError = 0 def __init__(self, name): self.name = name Player.hit+=1 Player.freePass+=1 Player.out+=1 Player.defenseError+=1 #-------------------------------- def main(): steve = Player("steve") steve.hit steve.hit steve.hit #-------------------------------- main() As you can tell, I have created a class that is supposed to increment a counter every time an instance is called in the main function. For example, 'steve.hit' is called three different times, so the hit counter should increment to 3. I have attempted many different ways of approaching this; but every time I try something, instead of counting the three different calls as such, the program will only count the three calls as one. Thank you for any help you can give me
steve.hit" isn't a "call", it's just accessing an attribute. That won't execute__init__. Either make it an actual function call (e.g.steve.hit()) in which you can do whatever you want, or make it a@propertywhich allows you to do a function call simply when doingsteve.hit.