View Single Post
Old 07-25-2014, 10:23 PM   #13
Join Date: Dec 2003
Location: Sydney, Australia
Originally Posted by SirBlurton View Post
I am an absolute beginner in python, so take this with a grain of salt - but what I've done in some of my tinkering has been to collect players in a list (ie-homeTeam contains all of the player objects for the home team).

Just makes it a bit cleaner when managing the function call.
For example:
def TopJumper(homeTeam,awayTeam)...

You could then loop through those lists to find the players with the best jumping ability.

Although I don't know yet how I would sort and loop through lists to automate this, this gave me the idea to fix a problem. What I had above was a function that determined who the best jumper was and returned those values in a variable. To run the actual jumpball I'd need to pass those variables and all the player ratings again to the next function and so on... Passing information between objects is something I need to work on.

What I did was create a list within the Team and Player classes, that builds a list from all the attributes that are stored in the class. I'm sure there's a better way to do this, but for now I manually enter them in as variables. To allow me to iterate through the created list, I had to define another function in the class. Team class is now as follows:

class Team(object): def __init__(self, location, city, name, record, arena): self.location = location = city = name self.record = record self.arena = arena self.list = [location, city, name, record, arena] #new list item def __getitem__(self,index): #magic that I don't understand, but it works. return self.list[index]

I don't quite understand how 'def __getitem__' works, but hey, Thanks Google! I can now pass a list of all attributes of a class through to a function by using, for example, the HomeTeam.list argument!

I don't think it makes sense either to have two separate functions to determine the best jumper and then the actual jumpball, so I combined them both in to one, using the lists created in the classes as follows.
Politics, n. Strife of interests masquerading as a contest of principles.
--Ambrose Bierce

Last edited by Groundhog : 07-25-2014 at 10:25 PM.
Groundhog is offline   Reply With Quote