Using __str__() in class object n python 3.7 [closed]

Like @Patrick Haugh mentioned in the comments, since the right hand side of the assignment is always executed first and then the assignment is evaluated with the result of that execution there is no (easy) way for the right hand side to know what the variable name would be.

Although not good, if you absolutely need to know this piece of information you can always pass the name as a parameter…

xyz = MyClass('xyz')

I believe there is a way of solving what you are asking with Metaprogramming, but since I have not experience in that area I wouldn’t dare to give you any advice

Leave a Comment