0

I have a class that I want to print it's name within a method

In the class file

class Example():
   def __init__(self,x):
      self.x = x
      if self.x == True:
         print(#Name of class object)

In main

from class_file.py import Example

abc = Example(True)

And when run I would like it to print "abc".

I have tried __name__ but that only returns "Example" (the class name) instead of the objects name.

Sachro
  • 11
  • 3
  • `abc` is *not* the "name of the object" in any way. It's the name of a variable which happens to refer to the object at some point in time. That's not reasonable to use for anything within the object itself. In fact, within `__init__`, `abc` doesn't even refer to the object yet, as the object isn't done being constructed and hasn't been assigned to `abc` yet. – deceze Jan 11 '23 at 16:11
  • A valid title would be: "Get variable name from within a class assigned to it" – RomanPerekhrest Jan 11 '23 at 16:14

0 Answers0