5
votes

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.

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)
3
you print a Speed_Camera object. What do you expect? - Daniel
Your code has indentation errors, and there is nothing called Speed_Camera defined. Also, you never said what you intend the program to do, so no one can tell you how to make it do what you want. - BrenBarn
@BrenBarn . I changed the name of the class to Camera from Speed_Camera without changing the rest of the code. Oops! Sorry :) - CoderLearner
the reason you get that string printed is because you are creating an instance of a class and prinitng it, but the class itself does not define what should be returned when you use print on it. also your functions within your class are not being called. - James Kent
@JamesKent so if I put something like " print (program.Calc_Speed()) i" it would work? - CoderLearner

3 Answers

12
votes

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.

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

"<__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.

1
votes

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.

0
votes

I agree with Sandford Basset You can try as well

program.Calc_Speed()