repr Questions

4

Solved

Implementing __repr__ for a class Foo with member variables x and y, is there a way to automatically populate the string? Example that does not work: class Foo(object): def __init__(self, x, y): ...
Promising asked 16/6, 2017 at 17:50

12

Solved

When you call the object.__repr__() method in Python you get something like this back: <__main__.Test object at 0x2aba1c0cf890> Is there any way to get a hold of the memory address if ...
Plus asked 23/9, 2008 at 14:35

28

Solved

What is the difference between __str__ and __repr__ in Python?
Carleencarlen asked 17/9, 2009 at 4:27

3

I've observed that when one types help in the Python repl, one gets Type help() for interactive help, ... and when one types help() one gets kicked into help mode. I'm pretty sure this is ...
Bioluminescence asked 21/2, 2013 at 23:5

0

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://jakeg...
Pender asked 28/5, 2022 at 14:43

1

Solved

Summary I have a dataclass with 10+ fields. print()ing them buries interesting context in a wall of defaults - let's make them friendlier by not needlessly repeating those. Dataclasses in Python Py...
Comyns asked 8/5, 2022 at 13:0

3

Solved

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 h...
Preordain asked 17/9, 2010 at 11:48

4

Solved

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...
Dedrick asked 4/6, 2012 at 1:36

5

Solved

__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 her...
Emolument asked 29/10, 2020 at 18:47

6

Solved

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 S...
Uncanny asked 13/10, 2011 at 15:32

0

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 repres...
Acronym asked 28/3, 2020 at 5:20

2

Solved

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 r...
Fowling asked 2/3, 2016 at 14:54

1

Solved

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 "MyExceptio...
Jari asked 8/1, 2020 at 9:53

9

Solved

What's the meaning of %r in the following statement? print '%r' % (1) I think I've heard of %s, %d, and %f but never heard of this.
Eyeless asked 1/3, 2010 at 7:13

1

Solved

I have a bunch of tables in SQLAlchemy that I want to define __repr__. The standard convention seems to look like this: def __repr__(self): return "<TableName(id='%s')>" % self.id This i...
Parrisch asked 16/4, 2019 at 17:30

4

Solved

I wrote a function to create the VALUES part of a SQL query: def query_values(data_iterator): return ',\n'.join('\n({})\n'.format(',\n'.join('"{}"'.format(value) for value in data_row) ) for dat...
Unwatched asked 19/3, 2019 at 14:16

3

Solved

How do I create a special method __repr__ where I can print, for example, '6 of spades' or 'Q of diamonds'? How do I access the data from the namedtuple, keeping in mind that I have a list of name...
Linnette asked 23/12, 2018 at 0:35

3

In python (3.5.2), I was expecting the repr(obj) function to call the magic method __repr__() of obj's class. However calling both of them do not seem to yield the same result. Can anyone explain ...
Poky asked 24/11, 2018 at 1:37

2

Solved

When I create a string containing backslashes, they get duplicated: >>> my_string = "why\does\it\happen?" >>> my_string 'why\\does\\it\\happen?' Why?
Sunlit asked 6/6, 2014 at 15:36

5

Solved

repr(): evaluatable string representation of an object (can "eval()" it, meaning it is a string representation that evaluates to a Python object) In other words: >>> x = 'foo' >>&g...
Liverwort asked 16/10, 2011 at 12:7

3

Solved

If someone writes a class in python, and fails to specify their own __repr__() method, then a default one is provided for them. However, suppose we want to write a function which has the same, or s...
Argive asked 13/2, 2018 at 22:52

2

Solved

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.
Granese asked 16/1, 2018 at 1:29

0

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...
Jones asked 22/11, 2017 at 11:3

3

Solved

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 ...
Stevenstevena asked 28/10, 2017 at 20:39

4

Solved

I call a __repr__() function on object x as follows: val = x.__repr__() and then I want to store val string to SQLite database. The problem is that val should be unicode. I tried this with no su...
Indebted asked 16/2, 2012 at 20:29

© 2022 - 2024 — McMap. All rights reserved.