What is the difference between `>>> some_object` and` >>> print some_object` in the Python interpreter?

In the interpreter, you can simply write the name of the object, for example. the list a = [1, 2, 3, u"hellรถ"] on the interpreter command line:

 >>> a [1, 2, 3, u'hell\xf6'] 

or you can do:

 >>> print a [1, 2, 3, u'hell\xf6'] 

which seems equivalent for lists. I am currently working with hdf5 to manage some data, and I realized that there is a difference between the two methods mentioned above. Given:

 with tables.openFile("tutorial.h5", mode = "w", title = "Some Title") as h5file: group = h5file.createGroup("/", 'node', 'Node information') tables.table = h5file.createTable(group, 'readout', Node, "Readout example") 

Conclusion

 print h5file 

differs from

 >>> h5file 

So, I was wondering if anyone could explain Python's behavioral differences in these two cases?

+6
source share
5 answers

Entering an object into the terminal calls __repr__() , which is intended to provide a detailed representation of the object you are printing (unambiguous). When you say "print", you call __str__() and therefore ask for something that is understandable to the person.

Alex Martelli gave a great explanation here . Other answers in the stream may also highlight the difference.

For example, look at datetime objects.

 >>> import datetime >>> now = datetime.datetime.now() 

Compare ...

 >>> now Out: datetime.datetime(2011, 8, 18, 15, 10, 29, 827606) 

to ...

 >>> print now Out: 2011-08-18 15:10:29.827606 

Hope this makes it more understandable!

+8
source

The interactive interpreter prints the result of each expression entered into it. (Since statements are not evaluated, but rather executed, this print behavior does not apply to statements such as print , loops, etc.).

Proof that repr() used by the interactive interpreter, as pointed out by Niklas Rosenstein (using the 2.6 interpreter):

 >>> class Foo: ... def __repr__(self): ... return 'repr Foo' ... def __str__(self): ... return 'str Foo' ... >>> x = Foo() >>> x repr Foo >>> print x str Foo 

So, while the print statement may not be necessary in the interactive interpreter (unless you need str rather than repr ), the non-interactive interpreter does not. Placing the above code in a file and running the file will result in nothing being printed.

+2
source

The print statement always calls the x.__str__() while method (only in the interactive interpreter), which simply calls the variable called by the x.__repr__() objects.

 >>> '\x02agh' '\x02agh' >>> print '\x02agh' 'agh' 
+1
source

Check out the Python documentation at: http://docs.python.org/reference/datamodel.html#object. repr

an object. magnesia (self)

 Called by the repr() built-in function and by string conversions 

(backticks) to evaluate the "official" string representation of an object. If at all possible, it should look like a valid Python expression that can be used to recreate an object with the same value (given the appropriate environment). If this is not possible, the form string <... some useful description ...> should be returned. The return value must be a string. If the class defines repr () but not str (), then repr () is also used when an โ€œInformalโ€ string representation of instances of this class is required.

 This is typically used for debugging, so it is important that the 
Representation

is informational and unambiguous.

an object. st (self)

 Called by the str() built-in function and by the print statement 

to compute an "informal" string representation of an object. This differs from repr () in that it should not be a valid Python expression: a more convenient or compressed representation may be used instead. The return value must be a string object.

Example:

 >>> class A(): ... def __repr__(self): return "repr!" ... def __str__(self): return "str!" ... >>> a = A() >>> a repr! >>> print(a) str! >>> class B(): ... def __repr__(self): return "repr!" ... >>> class C(): ... def __str__(self): return "str!" ... >>> b = B() >>> b repr! >>> print(b) repr! >>> c = C() >>> c <__main__.C object at 0x7f7162efb590> >>> print(c) str! 

The print function prints all __str__ arguments to the console. Like print(str(obj)) .

But in the interactive console, return the value of the __repr__ function. And if __str__ not defined, you can use __repr__ .

Ideally, __repr__ means we should just use this view to play this object. It should not be identical between different classes or an object representing different values. For example, datetime.time:

But __str__ (what we get from str(obj) ) should seem nice, because we are showing it to the user.

 >>> a = datetime.time(16, 42, 3) >>> repr(a) 'datetime.time(16, 42, 3)' >>> str(a) '16:42:03' #We dont know what it is could be just print: >>> print("'16:42:03'") '16:42:03' 

And, sorry for the bad English :).

0
source

print(variable) is equal to print(str(variable))

then

variable equals print(repr(variable))

Obviously, the __repr__ and __str__ of the h5file object give different results.

0
source

Source: https://habr.com/ru/post/895368/


All Articles