Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<__main__. object at 0x02C08790>

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)
like image 778
CoderLearner Avatar asked Jun 14 '15 18:06

CoderLearner


3 Answers

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.

like image 63
Ecko Avatar answered Oct 19 '22 01:10

Ecko


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.

like image 33
Sanford Bassett Avatar answered Oct 19 '22 02:10

Sanford Bassett


I wrote this answer for a linked closed duplicate. Since it goes into more detail about why the output looks like this, and this is apparently the canonical question, I'm putting it here.

Because you are printing an object, and that object doesn't have a __repr__ or __str__ method explaining how to print it.

Implementing these methods will let you print a 'description' of your object.

Background

When you call print(thing) in python, print looks to see if thing has a __str__ method, and calls it if it does. If it doesn't, it looks to see whether thing has a __repr__ method, and calls that if it does. If not, it keeps looking up the class hierarchy,* until it ends up with object, from which all user defined classes inherit (nowadays...). That's the method you're seeing here.

We can try this out:

>>> "I am a string".__str__()
"I am a string"
>>> "I am a string".__repr__()
"'I am a string'"

Yes! strings are objects in python. In fact, everything is an object.

>>> obj = object() # base class for all classes
obj.__str__()
'<object object at 0x7a87787987979877>'

So it's the fallback .__str__, which just prints that it's a thing at such and such a ram address.

*alright, it'll apply MRO, but I was keeping it simple.

like image 20
2e0byo Avatar answered Oct 19 '22 00:10

2e0byo