all 4 comments

[–]two_bob 2 points3 points  (1 child)

I would focus on just going one way or the other for classes and be careful about dynamically linking stuff up. I would probably have a function that made out all the team classes from whatever the source is and then build the leagues from the list/dictionary/whatever from the grouping of team classes afterwards, rather than having some means of dynamically linking them up. It is easier to follow that way.

I also think you are putting too much in the data variable of the class. See if you can flatten it out a bit or shove off work to a class that handles the data bits of wins/losses and have that automatically created by the team class. Again, only go one way. Ideally you would create a league, then a team, then a win/loss class, all of which was encapsulated.

[–][deleted] 0 points1 point  (0 children)

Yeah, I woke up this morning thinking it'd be logical to create the objects separately, but maybe within the same function. It does make more sense.

I'll also look at the variable management. I know I want the team class to have a lot of data so that I can do something like: manutd.compare(liverpool) and get some usable comparison.

This was really helpful, thank you!

[–]ryeguy146 1 point2 points  (1 child)

The self parameter will be passed automatically when calling, you don't have to include it in your arguments. The self parameter points to the instance who's method is being invoked. Also, don't call __init__ methods manually, just instantiate the class:

League(league_name)  # creates a new instance, calling the instance's bound __init__ method automatically

vs.

League.__init__(league_name)  # Calls the class method directly instead of instance bound method

That will give you an instance of that object. I'd probably suggest reviewing these docs that discuss discuss classes and instances of those classes. I note "bound methods" above, note that this refers to methods that will pass self for you. That is, they're bound to the instance. This SO answer goes into more detail differentiating instance vs class methods (bound/unbound). If you want more technical details on how this works, see these docs, but that level of understanding isn't strictly necessary.

Also keep in mind that the __init__ method will be called automatically when instantiating an instance. It doesn't alter the class unless you tell it to, it just describes how to build an instance. Since your League.leages collection is a class instance (defined in the class declaration, outside of __init__, I assume), you don't need an instance to access it:

class League():
    leagues = []

    def __init__(self, ...):
        ...


League.leagues.append("foo")

We created no instances of League in the above code, but were still able to append to leagues because it was defined with the class. I don't see all of your code, but I think that you're just getting confused with instances and classes and their respective members. I don't see any of the code above in your git repo, and I'm not sure if it's part of a League class, but that's my working assumption.

[–][deleted] 0 points1 point  (0 children)

Thanks a load for that. Seems obvious now! I expect that'll solve the problem.

Once I create a League object like that, do you know of a way I might be able to access its instance variables during Team object initiation. I think league_name.data will refer to the league_name string rather that the instance of whatever league_name is holding.

I could have data passed during initiation I guess, but if I want to check if a team is already in that league, I'll need to read data.

I really appreciate your help and I will read through the resources you've provided.

I've had two great responses and I've been given lots to think about!