Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the variable name of a Python object class [duplicate]

I would like to know if this is possible/how it is possible.

I have a Python object class:

class myClass(object):
    def __init__(self):
        ...do something

Then I create an instance of that object:

myObjectClass = myClass()

From within myClass, is it possible to get the "name" of the variable (myObjectClass) as a string?

Thanks, Adam

like image 375
user2387315 Avatar asked Nov 21 '25 11:11

user2387315


1 Answers

No, it isn't possible, in any reasonable manner, and also multiple variables may point to the same object. It's also not possible at all in respect of local variables. Whatever problem you're actually trying to solve, this is the wrong way to go about it.

Also, sort out your understanding of the difference between an object or instance and a class.

like image 191
Marcin Avatar answered Nov 23 '25 23:11

Marcin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!