Home > Net >  Object programming issue
Object programming issue

Time:12-02

I'm trying to solve this exercise. My desire output is to print the dictionary with value :name of "Trasa" and list of every Warszawa1 Warszawa 2 etc. For this I created the second function : stat_info. Current output: {'Name and information': ['Ko', <function station.stat_info at 0x7fe4dbbe3040>]} Desire output : {'Name and information': ['Ko',[Warszawa1, Warszawa 2 etc.] My code:

class station:
    def __init__(self, name, possibility_of_change, time_of_stay):
        self.name = name
        self.possibility_of_change = possibility_of_change
        self.time_of_stay = time_of_stay
        stat = []
        stat.append(self.name)
        self.stat = stat

    def stat_info(self):
        return self.stat



class Position_of_route:
    def __init__(self, station, time_between_2_stays):
        self.station = station
        self.time_between_2_stays = time_between_2_stays


class Route(station):
    def __init__(self, name1, list_of_positions):
        self.name1 = name1
        self.list_of_postitions = list_of_positions
    
    def trasa_info(self):
        return 5


    def generate(self, time):
        przystanki = [
            station("Warszawa1",True,3),
            station("Warszawa2",True,5),
            station("Warszawa3",False,0),
            station("Warszawa4",True,3),
            station("Warszawa5",True,7),
            station("Warszawa6",True,3),
            station("Warszawa7",True,9),
            station("Warszawa8",False,0),
            station("Warszawa9",True,12),
        ]
        a={
            "Name and information": [self.name1,station.stat_info]

        }
        return a
        
print(Route("Ko",[1,2]).generate(18))

CodePudding user response:

You are not calling the station.stat_info function but just storing its reference. In any case, this is a class method, and what you will need to call is the equivalent instance method using the instances of station which you have, which are stored on the przystanki list. For example:

a = { "Name and information": [self.name1,
                              [przystanek.stat_info() for przystanek in przystanki]] }

In this example, you would get a list of lists at the relevant place inside your data structure:

{'Name and information': ['Ko', [['Warszawa1'], ['Warszawa2'], ['Warszawa3'], ['Warszawa4'], ['Warszawa5'], ['Warszawa6'], ['Warszawa7'], ['Warszawa8'], ['Warszawa9']]]}

The question is a little ambiguous as to the exact output format required, and the calling code may differ if you wish to join together the lists, for example:

        info = []
        for przystanek in przystanki:
            info.extend(przystanek.stat_info())
        a = {                        
            "Name and information": [self.name1, info]
        }

if you wanted instead to have:

{'Name and information': ['Ko', ['Warszawa1', 'Warszawa2', 'Warszawa3', 'Warszawa4', 'Warszawa5', 'Warszawa6', 'Warszawa7', 'Warszawa8', 'Warszawa9']]}

but in any case you will be calling these instance methods.


Note on variable names: przystanki means "stations" in Polish, so I chose przystanek (meaning "station" singular) for the loop variable.

  • Related