4

At the IDLE interpreter I do the following with dpkt:

for ts, buf in pcap:
  eth = dpkt.ethernet.Ethernet(buf)

Now, when I try to see the contents of 'eth' I can either print it, or just write the variable name.

When I do:

print eth

I get:

O&áÿE(r @,òÀ¨
DYP?Jò}PªpÉ

However, when I simply write:

eth

I get the more expected output of:

Ethernet(src='<removed>', dst='<removed>', data=IP(src='<removed>', off=16384, dst='<removed>', sum=11506, len=40, p=6, ttl=128, id=29344, data=TCP(seq=2527752393, ack=218580057, win=16202, sum=62077, flags=16, dport=80, sport=51626)))

So my question is, what's the fundamental difference between doing a "print (variable)" and just writing the variable name? If I do a simple assignment (ie. "x = 100") I'll get a result of "100" for both "print x" and "x"

Rauffle
  • 957
  • 2
  • 8
  • 14
  • This is a duplicate of the question linked above. IDLE's Shell closely imitates interactive Python in a console window. IDLE sends what you enter to Python and displays the result returned by Python. – Terry Jan Reedy Jul 12 '17 at 22:39
  • Possible duplicate of [What is the difference between \`>>> some\_object\` and \`>>> print some\_object\` in the Python interpreter?](https://stackoverflow.com/questions/7114675/what-is-the-difference-between-some-object-and-print-some-object-in) – Georgy Jul 04 '19 at 09:34

2 Answers2

10

print(variable) equals to print(str(variable))

whereas

variable equals to print(repr(variable))

My guess is that the __repr__ and __str__ method of the class dpkt.ethernet.Ethernet produce these completely different results.

Update: Having a look at the source code tells me I am right.

gecco
  • 17,969
  • 11
  • 51
  • 68
  • On another note, I originally attempted to add the 'dpkt' tag to this question but was unable to because it would be a new tag and adding a new tag requires 1500+ reputation. Seeing as how your answer to my question has put you over 1500 rep, would you be able to add the 'dpkt' tag to my question (should you feel it to be appropriate)? – Rauffle Dec 23 '11 at 23:03
  • I don't feel it appropriate. The `str`/`repr` thing is pure pythonic, nothing to do with `dpkt`. You could rephrase the question to ask specifically for printing `dpkt.ethernet.Ethernet`, then it could be appropriate in my eyes... – gecco Dec 23 '11 at 23:08
  • Maybe we need a canonical question for this topic - any good standard class for such a question? – Douglas Leeder Dec 24 '11 at 12:14
  • Not the case for None value? For a None variable, typing the variable doesn't print anything. Yet `print(repr(None))` gives `None`. Why? – flow2k Jun 13 '18 at 19:14
3

There are two functions for representing data as a string in python: repr() and str().

When you use a print statement, the str function is called on whatever arguments you supplied for the print statement (and a newline is appended to the end of the result). For example, x = 100; print x will call str(x). The result ("100") will have a newline appended to it, and it will be sent to stdout.

When you execute something other than a print statement, the interpreter will print whatever value the expression yields using repr, unless the value is None, in which case nothing is printed.

In most cases, there are only subtle differences between the two. Objects, however, often define their own non-identical __str__ and __repr__ methods (which define the behavior for the str and repr built-in functions for that object). In your example, the eth object's __repr__ method must be different from the __str__ method.

I would speculate that the __str__ method is returning a binary string representation of the object to send across a network, but I can't be sure.

HardlyKnowEm
  • 3,196
  • 20
  • 30