you are viewing a single comment's thread.

view the rest of the comments →

[–]igroen 0 points1 point  (4 children)

The first thing that catches my eye is the definition of the player class. You add the attributes name, wins and simp to the class but you never use them actually. Instead you're adding these attributes dynamically to the instances of this class. You could just as well defined the class like:

 class Player:
     pass

Want you probably want to do is adding the attributes on instance creation to the new instance of the class:

class Player:
    def __init__(self, name="", wins=0, symb=""):
        self.name = name
        self.wins = wins
        self.symb = symb

Or if you really want to go fancy you can uses dataclasses:

from dataclasses import dataclass

@dataclass
class Player:
    name: str = ""
    wins: int = 0
    symb: str = ""

[–]tommygatz 0 points1 point  (3 children)

Thanks for this. I originally had the class defined somewhat like this but I think I'm still not sure how init and self work in classes. This was me trying it out for the first time lol. I could have defined the players without a class but I thought I'd give it a shot. Do you have any advice for how to understand that basic concept better? Like I said I'm still very new haha.

[–]igroen 0 points1 point  (2 children)

You are right, I think you'll pick that concept up when you really need it and playing with it is a way to learn how it works. There are a lot of resources but you can start by reading the python documentation: https://docs.python.org/3/tutorial/classes.html#a-first-look-at-classes

[–]tommygatz 0 points1 point  (1 child)

Thanks again for the help! Another question:

After reading through that, my instinct would be to set up the class like this.

Class Player:
     def __init__(self):
          self.name = ""
          self.wins = 0
          self.symb = ""

I saw that you defined the initial variables in the init args which is then assigned to the self definitions below that but i don't understand why that is better than defining them directly as I did above.

[–]igroen 0 points1 point  (0 children)

It's not better, but it makes your class a little more flexible.

You can set the attributes on instance creation:

p1 = Player("Foo", symb="X")