0

I want to use an object of a class in a function of another class. I know it's a really easy thing to solve, but I still can't figure it out.

I have the following class that is an object of players

class Players():
    def __init__(self, name_player, surname, age, height, wheight):
        self.name_player= name_player
        self.surname = surname
        self.age= age
        self.height= height
        self.wheight= wheight
    def __repr__(self):
        return "{}, {}, {}, {}, {} //".format(self.name_player,self.surname,self.age,self.height,self.wheight)

Then I have this class of teams

class Teams():
    def __init__(self, name, points, goals_in_favour, goals_against):
        self.name= name
        self.points= points
        self.goals_in_favour= goals_in_favour
        self.goals_against= goals_against

    def __repr__(self):
        return "{} : {} : {}: {}".format(self.name,self.points,self.goals_in_favour,self.goals_against)

Now in the function 'ages' I want it to return a list of the players with an age less or equal to the one on the file. If I write 'i.Players.age' it sends me an error, which is the right way to spell it?

def ages(lists_teams,age):
    list_players =""
    return [list_players + i for i in list_teams if (i.Players.age <= age)]

(I have to write it in a list comprehension way).

Thanks a lot in advance.

  • You donot need call class. i.Jugadores.edad, try change it to i.Jedad. i is already an class object here. Another mistake is lista_jugadores should be a list instead of a string? – Marcus.Aurelianus Jul 12 '18 at 01:06
  • Just a small comment on style- the variable name `i` is [traditionally used to indicate an index](https://stackoverflow.com/questions/4137785/why-are-variables-i-and-j-used-for-counters), not an the object in the list, so it can be a little confusing the way you have used it. – Ben Jones Jul 12 '18 at 01:08
  • For the function `edades`, is `lista_selectiones` supposed to be a List of `Selection` objects? – Ben Jones Jul 12 '18 at 01:17
  • What do you mean by list comprehension way, i is an object, while list_players is string. You cannot add object to a string...... – Marcus.Aurelianus Jul 12 '18 at 01:25

1 Answers1

1

To help you understand.

1.If list_teams is already an list of objects...you cannot call i.Players, because i here is already an object.

2.list_players="" you should change it to []...."" is not a list, you cannot add object on to it.

3.Try not use i for object, it is normally used to indicate an index..

def ages(list_teams,age):
    list_players =[]
    return [list_players + [player] for player in list_teams if (player.age <= age)]
Marcus.Aurelianus
  • 1,520
  • 10
  • 22