Getting the Class Name of an Instance

Getting the class name of an instance

Have you tried the __name__ attribute of the class? ie type(x).__name__ will give you the name of the class, which I think is what you want.

>>> import itertools
>>> x = itertools.count(0)
>>> type(x).__name__
'count'

If you're still using Python 2, note that the above method works with new-style classes only (in Python 3+ all classes are "new-style" classes). Your code might use some old-style classes. The following works for both:

x.__class__.__name__

Java - get the current class name?

The "$1" is not "useless non-sense". If your class is anonymous, a number is appended.

If you don't want the class itself, but its declaring class, then you can use getEnclosingClass(). For example:

Class<?> enclosingClass = getClass().getEnclosingClass();
if (enclosingClass != null) {
System.out.println(enclosingClass.getName());
} else {
System.out.println(getClass().getName());
}

You can move that in some static utility method.

But note that this is not the current class name. The anonymous class is different class than its enclosing class. The case is similar for inner classes.

Getting class name of instance and using it to create new instances in JS

claName is a string, not a function. The constructor function is inst1.constructor, call that.

class Test {
constructor() {
console.log("constructing a Test");
}
}

inst1 = new Test();
cls = inst1.constructor;
inst2 = new cls;

Get the class name of ES6 class instance

someClassInstance.constructor.name is exactly the correct way to do this. Transpilers may not support this, but it is the standard way per the specification. (The name property of functions declared via ClassDeclaration productions is set in 14.5.15, step 6.)

How to get the class name from an instance variable in Rails?

Use @commentable.class.name to find out the variable's class name.

Creating an instance using the class name and calling constructor

Yes, something like:

Class<?> clazz = Class.forName(className);
Constructor<?> ctor = clazz.getConstructor(String.class);
Object object = ctor.newInstance(new Object[] { ctorArgument });

That will only work for a single string parameter of course, but you can modify it pretty easily.

Note that the class name has to be a fully-qualified one, i.e. including the namespace. For nested classes, you need to use a dollar (as that's what the compiler uses). For example:

package foo;

public class Outer
{
public static class Nested {}
}

To obtain the Class object for that, you'd need Class.forName("foo.Outer$Nested").

Getting an instance name inside class __init__()

Instances don't have names. By the time the global name ThisObject gets bound to the instance created by evaluating the SomeObject constructor, the constructor has finished running.

If you want an object to have a name, just pass the name along in the constructor.

def __init__(self, name):
self.name = name

Python get class name

The only way to actually do what you are trying to during the class definition is to use a metaclass:

def saveDirMeta(name, bases, dct):
dct['saveDirectory'] = os.path.join(saveDir, name)
return type(name, bases, dct)

class TestClass(object):
__metaclass__ = saveDirMeta # this adds the 'saveDirectory' attribute
def __init__(self):
pass # do something

Or on Python 3.x:

class TestClass(metaclass=saveDirMeta):
def __init__(self):
pass # do something

This is definitely less clear than just using the following:

class TestClass():
saveDirectory = os.path.join(saveDir, 'TestClass')
def __init__(self):
pass # do something


Related Topics



Leave a reply



Submit