repr

When is the output of repr useful?

可紊 提交于 2019-12-04 08:05:00
I have been reading about repr in Python. I was wondering what the application of the output of repr is. e.g. class A: pass repr(A) ='<class __main__.A at 0x6f570>' b=A() repr(b) = '<__main__.A instance at 0x74d78>' When would one be interested in '<class __main__.A at 0x6f570>' or '<__main__.A instance at 0x74d78>' ? unutbu Sometimes you have to deal with or present a byte string such as bob2='bob\xf0\xa4\xad\xa2' If you print this out (in Ubuntu) you get In [62]: print(bob2) bob𤭢 which is not very helpful to others trying to understand your byte string. In the comments, John points out that

How can a string representation of a NumPy array be converted to a NumPy array?

﹥>﹥吖頭↗ 提交于 2019-12-02 07:21:49
问题 The function numpy.array_repr can be used to create a string representation of a NumPy array. How can a string representation of a NumPy array be converted to a NumPy array? Let's say the string representation is as follows: array([-0.00470366, 0.00253503, 0.00306358, -0.00354276, 0.00743946, -0.00313205, 0.00318478, 0.0074185 , -0.00312317, 0.00127158, 0.00249559, 0.00140165, 0.00053142, -0.00685036, 0.01367841, -0.0024475 , 0.00120164, -0.00665447, 0.00145064, 0.00128595, -0.00094848, 0

Python change repr floating digits

安稳与你 提交于 2019-12-02 04:28:35
问题 Okay, I want to use repr() to print out a text version of a bunch of lists and nested arrays. But I want the numbers to have only 4 decimal places not: 42.7635745114 but 32.7635. I'd like to use repr() because of its nice ability to handle nested arrays. Writing my own print loop is an unattractive option. Surely there is some way to overload repr to do this? I see there is a repr and reprlib modules but examples are really scarce, like nonexistent. 回答1: No, there is no way to overload repr()

Understanding difference between Double Quote and Single Quote with __repr__()

柔情痞子 提交于 2019-12-01 02:59:42
问题 What is the difference between print , object , and repr() ? Why is it printing in different formats? See the output difference : >>> x="This is New era" >>> print x # print in double quote when with print() This is New era >>> x # x display in single quote 'This is New era' >>> x.__repr__() # repr() already contain string "'This is New era'" >>> x.__str__() # str() print only in single quote '' 'This is New era' 回答1: There is no semantic difference between ' and " . You can use ' if the

What does !r do in str() and repr()?

时光毁灭记忆、已成空白 提交于 2019-11-30 03:59:41
According to the Python 2.7.12 documentation : !s (apply str() ) and !r (apply repr() ) can be used to convert the value before it is formatted. >>> import math >>> print 'The value of PI is approximately {}.'.format(math.pi) The value of PI is approximately 3.14159265359. >>> print 'The value of PI is approximately {!r}.'.format(math.pi) The value of PI is approximately 3.141592653589793. Interestingly, the converted value is the output of repr() , rather than str() . >>> str(math.pi) '3.14159265359' >>> repr(math.pi) '3.141592653589793' So what does "convert the value" mean here? Making it

Force repr() to use single quotes

梦想的初衷 提交于 2019-11-29 11:24:58
I have a question, is there a way to "force" repr() to create always single quotes around a string? This happens when I only use repr() print repr("test") 'test' print repr("test'") "test'" print repr("test\"") 'test"' print repr("test'\"") 'test\'"' so the last one actually does, what I want, but I don't want to add always \\" to get the single quotes. Edit: I am not going to mark an answer as accepted since, as pointed out by @martijn-pieters, I was using repr() for purposes it is not intended for. Well, if your object is always a string you could do this: def repr_single(s): return "'" +

Reverse repr function in Python

偶尔善良 提交于 2019-11-29 09:33:20
if I have a string with characters ( 0x61 0x62 0xD ), the repr function of this string will return 'ab\r' . Is there way to do reverse operation: if I have string 'ab\r' (with characters 0x61 0x62 0x5C 0x72 ), I need obtain string 0x61 0x62 0xD . I think what you're looking for is ast.literal_eval : >>> s = repr("ab\r") >>> s "'ab\\r'" >>> from ast import literal_eval >>> literal_eval(s) 'ab\r' 来源: https://stackoverflow.com/questions/24886123/reverse-repr-function-in-python

Java equivalent of Python repr()?

与世无争的帅哥 提交于 2019-11-29 09:06:19
Is there a Java method that works like Python's repr? For example, assuming the function were named repr, "foo\n\tbar".repr() would return "foo\n\tbar" not foo bar as toString does. In some projects, I use the following helper function to accomplish something akin to Python's repr for strings: private static final char CONTROL_LIMIT = ' '; private static final char PRINTABLE_LIMIT = '\u007e'; private static final char[] HEX_DIGITS = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; public static String toPrintableRepresentation(String source) { if(

Error with T::iterator, where template parameter T might be vector<int> or list<int>

社会主义新天地 提交于 2019-11-29 02:28:35
I'm trying to write a function to print a representation of common STL containers (vector, list, etc..). I gave the function a template parameter T which, for example, might represent vector. I'm having problems getting an iterator of type T. vector<int> v(10, 0); repr< vector<int> >(v); ... template <typename T> void repr(const T & v) { cout << "["; if (!v.empty()) { cout << ' '; T::iterator i; for (i = v.begin(); i != v.end()-1; ++i) { cout << *i << ", "; } cout << *(++i) << ' '; } cout << "]\n"; } ... brett@brett-laptop:~/Desktop/stl$ g++ -Wall main.cpp main.cpp: In function ‘void repr

What does !r do in str() and repr()?

给你一囗甜甜゛ 提交于 2019-11-28 22:55:44
问题 According to the Python 2.7.12 documentation: !s (apply str() ) and !r (apply repr() ) can be used to convert the value before it is formatted. >>> import math >>> print 'The value of PI is approximately {}.'.format(math.pi) The value of PI is approximately 3.14159265359. >>> print 'The value of PI is approximately {!r}.'.format(math.pi) The value of PI is approximately 3.141592653589793. Interestingly, the converted value is the output of repr() , rather than str() . >>> str(math.pi) '3