question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add good __repr__ methods to all public classes

See original GitHub issue

What should we add?

Informative reprs are very helpful, especially for debugging purposes. Ideally, repr(obj) should yield executable code that reconstructs obj, and we could test this. Good reprs 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:open
  • Created a year ago
  • Reactions:1
  • Comments:23 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
jakelishmancommented, Sep 8, 2022

Given that it’s an alternate class method constructor, I’m still not sure it would be appropriate for a repr (and Instruction, Qubit and Clbit don’t general have a evalable reprs either, which would be required). The point of repr 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, an evalable 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.

1reaction
jakelishmancommented, Aug 22, 2022

I’ve tagged this “good first issue”, though we wouldn’t want one PR to add reprs to every class - that would be too much work, and a good repr 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 to QuantumCircuit that returns a string like

<QuantumCircuit '<name>' with <x> qubits, <y> clbits and <z> instructions>
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found