+ 1

Question about Python Classes

I have to create a python program using classes and I have a question on whether some definitions should be separate from the class or not. My program will be a casino class with different types of games. Then I'm going to have another class to create a player. My question is whether the games such as blackjack and roulette should be within the class or separate. I am having a difficult time figuring out how the program structure should be. If it helps, this is just what I have so far let me know if there is anything I should change class Casino: def __init__(self, casinos_balance, wins = 0, loses = 0): self.casinos_balance = casinos_balance self.wins = wins self.loses = loses def __repr__(self): return '{} {} {} {} {}'.format(self.casinos_balance, self.wins, self.loses) def money_losed(self, money): self.casinos_balance -= money self.loses += 1 def money_won(self, money): self.casinos_balance += money self.wins += 1

13th Jul 2022, 2:27 AM
David
David - avatar
2 Respostas
+ 1
This is a question of domain modeling. In more classical OOP languages like Java, you cannot write any code that is outside of a class. However Python is more liberal in this regard. You can combine classes, functions outside classes, and even free-standing statements in the same python program. Determining the best structure is not always trivial, and takes some practice. There are some generic guidelines such as DRY and KISS, but the practical implication is sometimes not clear. What I would suggest, is maybe a little against the usual OOP design, where data (fields) and behavior (methods) are closely coupled within the same class. You can write Python code according to functional paradigm, where your classes only contain data, and you have functions (not necessarily class methods) that operate on those class instances. But maybe this approach is not what your lecturer expects from you.
13th Jul 2022, 7:24 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Tibor Santa Thank you for taking the time to help me with this. Your answer helps alot. Really Appreciate it.
13th Jul 2022, 1:50 PM
David
David - avatar