本文介绍了<__main__.0x02C08790处的对象>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在接受

<__main__.Camera object at 0x02C08790>

我不知道为什么.

我希望代码基本上在循环中从 Calc_Speed 转到 Counter,然后返回到 Calc_Speed.

I would like the code to go from Calc_Speed to Counter and then back to Calc_Speed basically in a loop.

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)

推荐答案

当你只是打印一个对象时,它会显示对象 id(例如 <__main__.Camera object at 0x02C08790>),即对我们凡人来说是完全无法理解的.您可以通过定义 __str____repr__ 函数来以自定义方式显示实例的数据来解决此问题.

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.

就你而言:

def __repr__(self):
    return "<__main__.Camera: distance = " + str(self.distance) + "; speed_limit = " + str(self.speed_limit) + "; number_of_cars = " + str(self.number_of_cars) + ">"

如果有一个带有起始变量值的 Camera 实例,它将返回

If there were an instance of Camera with the starting variable values, it would return

"<__main__.Camera: distance = 2; speed_limit = 20; number_of_cars = 0>".

<__main__.Camera object at 0x02C08790> 是系统记住它的方式,但除了显示它是什么类型的对象之外,它几乎没有用.

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.

这篇关于&lt;__main__.0x02C08790处的对象>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:32