Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Pythonic" way to "reset" an object's variables?
    primarykey
    data
    text
    <p><em>("variables" here refers to "names", I think, not completely sure about the definition pythonistas use)</em></p> <p>I have an object and some methods. These methods all need and all change the object's variables. How can I, in the most pythonic and in the best, respecting the techniques of OOP, way achieve to have the object variables used by the methods but also keep their original values for the other methods?</p> <p>Should I copy the object everytime a method is called? Should I save the original values and have a reset() method to reset them everytime a method needs them? Or is there an even better way?</p> <p><strong>EDIT:</strong> I was asked for pseudocode. Since I am more interested in understanding the concept rather than just specifically solving the problem I am encountering I am going to try give an example:</p> <pre><code>class Player(): games = 0 points = 0 fouls = 0 rebounds = 0 assists = 0 turnovers = 0 steals = 0 def playCupGame(self): # simulates a game and then assigns values to the variables, accordingly self.points = K #just an example def playLeagueGame(self): # simulates a game and then assigns values to the variables, accordingly self.points = Z #just an example self.rebounds = W #example again def playTrainingGame(self): # simulates a game and then assigns values to the variables, accordingly self.points = X #just an example self.rebounds = Y #example again </code></pre> <p>The above is my class for a Player object (for the example assume he is a basketball one). This object has three different methods that all assign values to the players' statistics.</p> <p>So, let's say the team has two league games and then a cup game. I'd have to make these calls:</p> <pre><code>p.playLeagueGame() p.playLeagueGame() p.playCupGame() </code></pre> <p>It's obvious that when the second and the third calls are made, the previously changed statistics of the player need to be reset. For that, I can either write a reset method that sets all the variables back to 0, or copy the object for every call I make. Or do something completely different.</p> <p>That's where my question lays, what's the best approach, python and oop wise?</p> <p><strong>UPDATE:</strong> I am suspicious that I have superovercomplicated this and I can easily solve my problem by using local variables in the functions. However, what happens if I have a function inside another function, can I use locals of the outer one inside the inner one?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload