How do I print the variable name holding my object?
For example, I have:
myclass ob=new myclass()
How would I print "ob"?
Print an Object in Python Using the __repr__() Method It, by default, returns the name of the object's class and the address of the object. When we print an object in Python using the print() function, the object's __str__() method is called.
In short. You can't print just the name of a variable.
The getName() method is used to get the names of the entities such as interface, class, array class, void etc. that are represented by the class objects.
names() function in R Language is used to get or set the name of an Object. This function takes object i.e. vector, matrix or data frame as argument along with the value that is to be assigned as name to the object. The length of the value vector passed must be exactly equal to the length of the object to be named.
Objects don't have names, unless you happen to be using a class which allows each object to be given one (e.g. via a variable retrieved with getName()
).
In particular, the name of any particular variable used to refer to an object is completely unknown to the object itself. So you can't do:
Object foo = new Object();
// There's no support for this
String name = foo.getName(); // expecting to get "foo"
(Bear in mind that several variables could all refer to the same object, and there don't have to be any named variables referring to an object.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With