Add good __repr__ methods to all public classes
See original GitHub issueWhat should we add?
Informative repr
s are very helpful, especially for debugging purposes. Ideally, repr(obj)
should yield executable code that reconstructs obj
, and we could test this. Good repr
s are defined inconsistently across the library. For example,
from qiskit import QuantumCircuit, QuantumRegister
print(repr(QuantumRegister(1)))
print(repr(QuantumCircuit(1)))
QuantumRegister(1, 'q0')
<qiskit.circuit.quantumcircuit.QuantumCircuit object at 0x7f372c1dfac0>
The following classes require attention:
- Gate
- QuantumCircuit
- Sampler
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:23 (13 by maintainers)
Top Results From Across the Web
repr - What are the best practices for __repr__ with ...
I have a custom Python class which essentially encapsulate ...
Read more >Python __str__() and __repr__() functions | DigitalOcean
This method is called when repr() function is invoked on the object. ... It's not a good idea to use these functions directly....
Read more >What is the __str__ method in Python? - Educative.io
The __str__ method should be defined in a way that is easy to read and outputs all the members of the class. This...
Read more >Python's property(): Add Managed Attributes to Your Classes
In this step-by-step tutorial, you'll learn how to create managed attributes, also known as properties, using Python's property() in your custom classes.
Read more >7. Classes and objects — Beginning Python Programming for ...
You can use dot notation as part of any expression, so the following statements are ... To solve this problem we add an...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Given that it’s an alternate class method constructor, I’m still not sure it would be appropriate for a
repr
(andInstruction
,Qubit
andClbit
don’t general have aeval
able reprs either, which would be required). The point ofrepr
is to give a human-readable indication of what the object is, rather than to implement a complete serialisation to text - it’s just that for small, simple objects, aneval
able string is a very legible description of the object.For
QuantumCircuit
, there’s loads of internal attributes like global phase, metadata, scheduling information, stored internal layout, etc etc that would make a “complete” textual serialisation super long and really hard to read. The simplified version I suggested above I think is still good.I’ve tagged this “good first issue”, though we wouldn’t want one PR to add
repr
s to every class - that would be too much work, and a goodrepr
needs a little bit of thought, so it’s not an entirely mechanical process. Instead, a good first community PR would be to add a__repr__
method toQuantumCircuit
that returns a string like