[英]Calling method from previous class
I'm trying to call a method from a previous Class for the Class I'm currently working on.我正在尝试为我目前正在处理的类调用前一个类的方法。 It's a mock GPS system using classes for different things.这是一个模拟 GPS 系统,使用不同的类。 The class I'm trying to get the method from looks like the following :我试图从中获取方法的类如下所示:
class GPS_POI:
def __init__(self, location, name , kind):
self.location= location
self.name = str(name)
self.kind = str(kind)
def __str__ (self):
return (str(self.location) + ": "+ self.name +", " + self.kind )
The current class and method I am working on:我正在研究的当前类和方法:
class GPS :
def __init__ (self, current, map = None):
self.current = current
self.map= map
self.route= []
def display_map(self):
for i in self.route:
display= GPS_POI()
return (display.__str__ + "\n")
When I run it, I just end up getting the result of "None" when the output I want (example) would be :当我运行它时,当我想要的输出(示例)是:
"(3,1): kmart, clothes \\n(2,3): burger king, food\\n" etc. “(3,1):kmart,衣服\\n(2,3):汉堡王,食物\\n”等。
Would I need to include my parameter self.map from the class GPS into the display_map function for it work properly?我是否需要将 GPS 类中的参数 self.map 包含到 display_map 函数中才能正常工作? What am I not understanding about calling a method from a previous class?我对从以前的类调用方法有什么不理解的?
You aren't calling display.__str__
;你不是在打电话display.__str__
; you are just referencing it.你只是在引用它。 You shouldn't call __str__
explicitly anyway.无论如何你都不应该显式地调用__str__
。 Its purpose is to provide a hook for when you try to treat the object as a string, such as when passing it to str
as an argument:它的目的是为当您尝试将对象视为字符串时提供一个钩子,例如将它作为参数传递给str
:
return str(display) + "\n"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.