Answers for "python get class name as string"

2

how to get the name of a class in python

class test:
  @property
  def cls_name(self):
    return self.__class__.__name__
  
  @property
  def cls_name_2(self):
    return type(self).__name__
  
  @classmethod 
	def cls_name_3(cls):
		return cls.__name__
      
ins = test()

print(ins.cls_name)
print(ins.cls_name_2)
print(ins.cls_name_3())
Posted by: Guest on September-10-2021
1

how to get name of class in class python

class SillyClassName:
    @classmethod 
	def my_name(cls_):
		return cls_.__name__

 def class_name(self):
  	# self.__class__ gets the current class
    # .__name__ gets the name
	return self.__class__.__name__

SillyClassName.my_name()
# prints SillyClassName

inst = SillyClassName()
inst.class_name()
# prints SillyClassName
Posted by: Guest on March-27-2020

Code answers related to "python get class name as string"

Python Answers by Framework

Browse Popular Code Answers by Language