Python <__主要__。对象在 0x02C08790>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30832999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
<__main__. object at 0x02C08790>
提问by CoderLearner
I keep getting
我不断得到
<__main__.Camera object at 0x02C08790>
and I don't know why.
我不知道为什么。
I would like the code to go from Calc_Speed to Counter and then back to Calc_Speed basically in a loop.
我希望代码从 Calc_Speed 到 Counter,然后基本上在循环中返回到 Calc_Speed。
class Camera():
distance = 2
speed_limit = 20
number_of_cars = 0
def Calc_Speed(self):
registration = input("Registration Plate: ")
Speeding_List=[]
start = float(input("Start time: "))
end = float(input("End Time: "))
speed = self.distance/(end-start)
print(("Average Speed: ") + str(round(speed, 2)) + (" mph"))
if speed > self.speed_limit:
list3= [str(self.registration)]
Speeding_List.append(list3)
print("Vehicles Caught Speeding: " + str(Speeding_List))
return(program.Counter())
else:
print("Vehicle Not Speeding")
return(program.Counter())
def Counter():
self.number_of_cars = self.number_of_cars + 1
print("Number Of Cars Recorded: " + str(self.number_of_cars))
return(program.Calc_Speed())
program = Camera()
print(program)
回答by Ecko
When you just print an object, it shows the object id (like <__main__.Camera object at 0x02C08790>
), which is totally indecipherable to us mortals. You can get around this by defining a __str__
or __repr__
function to display the data for the instance in a custom way.
当您只是打印一个对象时,它会显示对象 id(如<__main__.Camera object at 0x02C08790>
),这对我们凡人来说是完全无法辨认的。您可以通过定义一个__str__
或__repr__
函数来以自定义方式显示实例的数据来解决这个问题。
In your case:
在你的情况下:
def __repr__(self):
return "<__main__.Camera: distance = " + str(self.distance) + "; speed_limit = " + str(self.speed_limit) + "; number_of_cars = " + str(self.number_of_cars) + ">"
If there were an instance of Camera
with the starting variable values, it would return
如果有一个Camera
具有起始变量值的实例,它将返回
"<__main__.Camera: distance = 2; speed_limit = 20; number_of_cars = 0>"
.
"<__main__.Camera: distance = 2; speed_limit = 20; number_of_cars = 0>"
.
The <__main__.Camera object at 0x02C08790>
is the how the system remembers it, but aside from showing what type of object it is, it's mostly useless.
这<__main__.Camera object at 0x02C08790>
是系统记住它的方式,但除了显示它是什么类型的对象之外,它几乎没有用。
回答by Muhammad Imran Khan
I had something like this def
我有这样的定义
def __toString__(self):
return "{} is {} cm tall and {} kilogram and self.__sound)
I just chnaged "toString" to "repr" and it works now
我只是将“toString”更改为“repr”,现在可以使用了
回答by Sanford Bassett
Rather than printing the object itself, you would want to print a function of that object. If you replaced the last line with:
与其打印对象本身,不如打印该对象的函数。如果您将最后一行替换为:
print(program.Calc_Speed())
The program would work more similarly to what you seem to be looking for.
该程序的工作方式与您似乎正在寻找的内容更相似。