abstracting class functions? /u/alexaluther96 Python Education

Hey, all~! I’m learning Python, and have a question about class functions.

If I’m expecting to have a lot of instances for a particular class, is there any benefit to moving the class functions to a separate module/file?

It’s a turn-based strategy game module, and each instance of the Character class needs the ability to attack other Character instances.

import turn_based_game as game
player1 = game.Character()
player2 = game.Character()

player1.attack(player2)
# OR
game.attack(player1, player2)

Which way is better? The second option of game.attack() seems like it would be a more lightweight solution since the function only exists once in the game module rather than in each instance~?

submitted by /u/alexaluther96
[link] [comments]

​r/learnpython Hey, all~! I’m learning Python, and have a question about class functions. If I’m expecting to have a lot of instances for a particular class, is there any benefit to moving the class functions to a separate module/file? It’s a turn-based strategy game module, and each instance of the Character class needs the ability to attack other Character instances. import turn_based_game as game player1 = game.Character() player2 = game.Character() player1.attack(player2) # OR game.attack(player1, player2) Which way is better? The second option of game.attack() seems like it would be a more lightweight solution since the function only exists once in the game module rather than in each instance~? submitted by /u/alexaluther96 [link] [comments] 

Hey, all~! I’m learning Python, and have a question about class functions.

If I’m expecting to have a lot of instances for a particular class, is there any benefit to moving the class functions to a separate module/file?

It’s a turn-based strategy game module, and each instance of the Character class needs the ability to attack other Character instances.

import turn_based_game as game
player1 = game.Character()
player2 = game.Character()

player1.attack(player2)
# OR
game.attack(player1, player2)

Which way is better? The second option of game.attack() seems like it would be a more lightweight solution since the function only exists once in the game module rather than in each instance~?

submitted by /u/alexaluther96
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *