Questions tagged [repr]

Python built-in function that returns a string from an arbitrary object. Abbreviation for "representation".

docs: https://docs.python.org/3/library/functions.html#repr

281 questions
7
votes
1 answer

Output difference between ipython and python

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([])
wim
  • 338,267
  • 99
  • 616
  • 750
6
votes
0 answers

Nativecall Segfaults Getting Tuple from Rust

Per the Rust FFI Omnibus the following should work. This is a rust cdylib lib.rs named "foo" made with cargo build... use std::convert::From; // Rust FFI Omnibus: Tuples // http://jakegoulding.com/rust-ffi-omnibus/tuples/ // A Rust function that…
librasteve
  • 6,832
  • 8
  • 30
6
votes
0 answers

How do I get the primitive representation of a generic enum in Rust?

I have a function that reads bits and creates an enum. Currently the caller needs to provide both the EnumType and the representation of this enum. How do I change the function such that the representation is deduced from the EnumType, such that the…
6
votes
0 answers

repr and super in Python 2.7. `super().__repr__` vs `repr(super())`

Question: Why is repr(super(class, thing)) different from super(class, thing).__repr__()? I've come across something that I think is odd, would be great if someone has an explanation for why this works the way it does. My question is about how repr…
user27182
  • 200
  • 3
  • 9
6
votes
3 answers

Matlab repr function

In Matlab, one can evaluate an arbitrary string as code using the eval function. E.g. s = '{1, 2, ''hello''}' % char c = eval(s) % cell Is there any way to do the inverse operation; getting the literal string representation of an…
jmd_dk
  • 12,125
  • 9
  • 63
  • 94
6
votes
4 answers

How to print __repr__ in a Python3 file? (not in shell)

I know that in Python Shell when you type >>> object it shows the object.__repr__ method and if you type >>> print(object) it shows the object.__str__ method. But my question is, is there a short way to print __repr__ while executing a Python…
YoomarXD
  • 105
  • 1
  • 10
6
votes
1 answer

Writing a repr method

Situation: I'm new to python and currently trying to learn the ropes, I've attempted creating a linked list class to assist in getting a better understanding of the language and its structures. I know that the __repr__ function is basically…
user6457225
6
votes
3 answers

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

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…
bretttolbert
  • 984
  • 1
  • 9
  • 16
6
votes
3 answers

__repr__() returned non-string

So I have a class method with which I would like to draw out the dictionary and it's values: def __repr__ (self): for row in zip(*([ky]+map(str,val) for ky,val in (self.slovar.items()))): print"\t".join (row) If it's like…
user1509923
  • 193
  • 1
  • 3
  • 13
6
votes
1 answer

Python accent graves bad practice?

I have recently come to understand that we can use the following as a shorthand for repr(x) in Python: `x` However, I have rarely seen this in practice. Is it considered to be bad practice or unpythonic? Or are there any other reasons for which it…
arshajii
  • 127,459
  • 24
  • 238
  • 287
5
votes
6 answers

Python __repr__ and None

I'm quite new to Python and currently I need to have a __repr__ for a SqlAlchemy class. I have an integer column that can accept Null value and SqlAlchemy converts it to None. For example: class Stats(Base): __tablename__ = "stats" description…
Patrizio Rullo
  • 491
  • 1
  • 4
  • 13
5
votes
5 answers

How to set a repr for a function itself?

__repr__ is used to return a string representation of an object, but in Python a function is also an object itself, and can have attributes. How do I set the __repr__ of a function? I see here that an attribute can be set for a function outside the…
Pranab
  • 2,207
  • 5
  • 30
  • 50
5
votes
1 answer

__repr__ for Exception derived class not works well

I tried to use __repr__ method on object that inherit from Exception. but nothing was printed! Can anyone help explain why? class MyException(Exception): def __repr__(self): return "MyException Object" try: raise…
Ori.B
  • 109
  • 6
5
votes
2 answers

How to convert a deeply nested list to a string

If I make a deeply nested list, like this: arr = [1] for i in range(1000): arr = [arr] then print(arr) will work fine, but str(arr) fails miserably with maximum recursion depth exceeded. ("%s" % arr, and repr(arr) too.) How could I get the…
vagoston
  • 171
  • 8
5
votes
2 answers

Is __repr__ supposed to return bytes or unicode?

In Python 3 and Python 2, is __repr__ supposed to return bytes or unicode? A reference and quote would be ideal. Here's some information about 2-3 compatibility, but I don't see the answer.
Hatshepsut
  • 5,962
  • 8
  • 44
  • 80
1 2
3
18 19