0

When I print deque object, all items are displayed. This is fine because I wanted to display all items But I was thinking object reference info will be displayed as below in this senario.

Question: How can you identify print(q) display all items by definition in deque source code

I just want to know the reason and which source code allow print(q) to display deque([1]) instead of <__main__.~ object at 0x~~~~~~~8>

I dont mean I want to display <__main__.~ object at 0x~~~~~~~8>

from collections import deque

q = deque()
q.append(1)
print(q) 


# Actual output          :  deque([1])
# Output in my mind(which I imagined)  :  <__main__.~ object at 0x~~~~~~~8>
Kazu25
  • 87
  • 1
  • 2
  • 13
  • 1
    Does this answer your question? [How can we get the default behavior of \_\_repr\_\_()?](https://stackoverflow.com/questions/48777014/how-can-we-get-the-default-behavior-of-repr) – Nullman Jan 07 '21 at 11:35
  • NO, My question is why actual deque value is print out like my example I just want to know the reason and which sourc code allow `print(q)` to display `deque([1])` instead of `<__main__.~ object at 0x~~~~~~~8>`. I dont mean I want to display `<__main__.~ object at 0x~~~~~~~8>` – Kazu25 Jan 07 '21 at 13:06
  • it IS the answer to that question. deque has a custom `__repr__`, if you dont define one the default one prints what you expect – Nullman Jan 07 '21 at 13:11
  • you are saying when I print out `print(q)`, It displays `<__main__.~ object at 0x~~~~~~~8>` as a default ??? – Kazu25 Jan 07 '21 at 13:17
  • when you try to print a class it will try to print the classes `__str__` and if that doesnt exist it will print the classes `__repr__` which defaults to what you mentioned. if you want just the number then you can get it with `id(q)` which will return the same number you see(but in decimal format instead of hexadecimal). you can read more about [__repr__](https://docs.python.org/3/reference/datamodel.html#object.__repr__) in the docs – Nullman Jan 07 '21 at 13:20
  • I just want to confirm again,,,,,, When I print(q) in my example, It displayed `deque([1])`,,,,,,, NOT `<__main__.~ object at 0x~~~~~~~8>`,,,,,,, Do you understand this right? – Kazu25 Jan 07 '21 at 13:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226975/discussion-between-kazu25-and-nullman). – Kazu25 Jan 07 '21 at 13:27

0 Answers0