How can my code discover the name of an object?

Generally speaking, it can’t, because objects don’t really have names. Essentially, assignment always binds a name to a value; the actual name is only known by the namespace it’s in, and a single value can be referenced from many different namespaces (as well as containers).

The same is true of def and class statements, but in that case the value is a callable. Consider the following code:

class A:
    pass

B = A

a = B()
b = a
print b
<__main__.A instanc