如何在Python中正确返回类实例列表

wis*_*ert 2 python class list

class ligne():
    def __init__ (self, stops):
        ##stops = a list of instances of Ligne1Stop class
        self.stops = stops
    def returnAllStopsOnLigne(self):
        return self.stops
Run Code Online (Sandbox Code Playgroud)

当我调用方法returnAllStopsOnLigne()时,我得到一个列表

"<__main__.ligne1Stop instance at 0x1418828">
Run Code Online (Sandbox Code Playgroud)

如何在停止列表中返回正确的类实例名称?

Mar*_*ers 9

您正在查看repr()类的表示输出.如果在自定义类上定义,repr()将调用__repr__()钩子:

def __repr__(self):
    return '<linge1Stop: name={0}>'.format(self.name)
Run Code Online (Sandbox Code Playgroud)