repr

Best output type and encoding practices for __repr__() functions?

喜夏-厌秋 提交于 2019-11-28 17:22:11
Lately, I've had lots of trouble with __repr__() , format() , and encodings. Should the output of __repr__() be encoded or be a unicode string? Is there a best encoding for the result of __repr__() in Python? What I want to output does have non-ASCII characters. I use Python 2.x, and want to write code that can easily be adapted to Python 3. The program thus uses # -*- coding: utf-8 -*- from __future__ import unicode_literals, print_function # The 'Hello' literal represents a Unicode object Here are some additional problems that have been bothering me, and I'm looking for a solution that

Output difference between ipython and python

非 Y 不嫁゛ 提交于 2019-11-28 14:19:00
It was my understanding that python will print the repr of the output, but this is apparently not always the case. For example: In ipython: In [1]: type([]) Out[1]: list In [2]: set([3,1,2]) Out[2]: {1, 2, 3} In python: >>> type([]) <type 'list'> >>> set([3,1,2]) set([1, 2, 3]) What transformation does ipython apply on the output? Instead of repr or standard pprint module IPython uses IPython.lib.pretty.RepresentationPrinter.pretty method to print the output . Module IPython.lib.pretty provides two functions that use RepresentationPrinter.pretty behind the scenes. IPython.lib.pretty.pretty

Possible to change a function's repr in python?

試著忘記壹切 提交于 2019-11-28 09:53:14
I've only seen examples for setting the __repr__ method in class definitions. Is it possible to change the __repr__ for functions either in their definitions or after defining them? I've attempted without success... >>> def f(): pass >>> f <function f at 0x1026730c8> >>> f.__repr__ = lambda: '<New repr>' >>> f <function __main__.f> Yes, if you're willing to forgo the function actually being a function. First, define a class for our new type: import functools class reprwrapper(object): def __init__(self, repr, func): self._repr = repr self._func = func functools.update_wrapper(self, func) def _

Reverse repr function in Python

南笙酒味 提交于 2019-11-28 03:11: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 . 回答1: 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-28 02:28:20
问题 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. 回答1: 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

In Python, what does '<function at …>' mean?

橙三吉。 提交于 2019-11-28 02:00:30
What does <function at 'somewhere'> mean? Example: >>> def main(): ... pass ... >>> main <function main at 0x7f95cf42f320> And maybe there is a way to somehow access it using 0x7f95cf42f320 ? You are looking at the default representation of a function object. It provides you with a name and a unique id, which in CPython happens to be a memory address. You cannot access it using the address; the memory address is only used to help you distinguish between function objects. In other words, if you have two function objects which were originally named main , you can still see that they are

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

做~自己de王妃 提交于 2019-11-27 16:43:14
问题 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 <<

str() vs repr() functions in python 2.7.5 [duplicate]

ⅰ亾dé卋堺 提交于 2019-11-27 11:48:43
This question already has an answer here: Difference between __str__ and __repr__? 22 answers what is the difference between str() and repr() functions in python 2.7.5? Explanation on python.org: The str() function is meant to return representations of values which are fairly human-readable , while repr() is meant to generate representations which can be read by the interpreter (or will force a SyntaxError if there is no equivalent syntax) But it wasn't clear for me. some examples: >>> s = 'Hello, world.' >>> str(s) 'Hello, world.' >>> repr(s) "'Hello, world.'" # repr is giving an extra double

Output difference between ipython and python

随声附和 提交于 2019-11-27 08:12:59
问题 It was my understanding that python will print the repr of the output, but this is apparently not always the case. For example: In ipython: In [1]: type([]) Out[1]: list In [2]: set([3,1,2]) Out[2]: {1, 2, 3} In python: >>> type([]) <type 'list'> >>> set([3,1,2]) set([1, 2, 3]) What transformation does ipython apply on the output? 回答1: Instead of repr or standard pprint module IPython uses IPython.lib.pretty.RepresentationPrinter.pretty method to print the output. Module IPython.lib.pretty

Possible to change a function's repr in python?

房东的猫 提交于 2019-11-27 03:21:46
问题 I've only seen examples for setting the __repr__ method in class definitions. Is it possible to change the __repr__ for functions either in their definitions or after defining them? I've attempted without success... >>> def f(): pass >>> f <function f at 0x1026730c8> >>> f.__repr__ = lambda: '<New repr>' >>> f <function __main__.f> 回答1: Yes, if you're willing to forgo the function actually being a function. First, define a class for our new type: import functools class reprwrapper(object):