import sys class X: def __init__( it, var ): it.var = var return def show( it ): print( f'{it.var = }' ) try: print( f'{var = }' ) except (NameError): sys.stderr.write( '(Should have qualified with the instance object!)\n' ) raise return X( var = 'value' ).show() # python3.11 x.py it.var = 'value' (Should have qualified with the instance object!) Traceback (most recent call last): File "/home/aa/tmp/python/x.py", line 18, in X( var = 'value' ).show() File "/home/aa/tmp/python/x.py", line 11, in show print( f'{var = }' ) ^^^ NameError: name 'var' is not defined. Did you mean: 'vars'?