F'{expr=}' without self

It is possible to omit “self.” in the output of self-documenting f-strings? I.e.

class A:
    def __init__(self):
        self.a = 1
        print(f"{self.a=}")

A()  # Desired output: 'a=1', actual output: 'self.a=1'

self.a is the name of the variable. This is both expected and correct.

I’m not saying it’s a bug, I’m asking if there is a way to make it not print self.

Write f"a={self.a!r}". The = is just a shorthand to simplify debugging. If it doesn’t fit your need, don’t use it.

2 Likes

Sure. I’d like to use it in a __repr__, but of course I can just do it the old-fashioned way.