I have this code, with the desired output "The value is Bar and the name is b."
class myClass:
def __init__(self, value):
self.value = value
b = myClass("Bar")
def foo(var):
true_name = str(var)
print("The value is %s and the name is %s" % (var.value, true_name))
foo(b)
However, this prints The value is Bar and the name is <__main__.myClass object at 0x000000187E979550>
, which is less useful to me.
Now, I know the problems with trying to get the true name of a variable in Python. However, I don't need to do any fancy introspection; I just want to convert the actual letters typed between the parentheses of foo( ) and print that.
To me, this sounds like a simple thing that would be useful in debugging and examining my code, so I can tell exactly what object did a thing. Am I making a fundamental error in my assumptions, and this is a terrible idea?