python: get name of instantiating class?

  • Last Update :
  • Techknowledgy :

You can't, unless you pass an instance of the 'creator' to the Class2() constructor. e.g.

class Class1(object):
   def __init__(self, * args, ** kw):
   self.x = Class2("Woo!", self)

class Class2(object):
   def __init__(self, word, creator, * args, ** kw):
   self._creator = creator
print word

Suggestion : 2

With a basic understanding of the Metaclass and objects in Python, let's now understand the object creation and initialization process in Python. Consider the Human class, as defined below:,In this article, we explored the __new__, __init__, and __call__ magic methods and discussed Metaclass in Python. In doing so, we have a better understanding of the object creation and initialization processes in Python.,Consider an example of instantiating an object in Python.,Internals of Object Instantiation and Metaclass in Python Table of Contents The object base class in Python3 Objects and types in Python Metaclass in Python The object instantiation process in Python The __new__ method Override the __new__ method The __init__ method The __call__ method callable() Conclusion References

class Human:

# Output: ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__',
   '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__',
   '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
   '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'
# Output: (<class 'object'>,)

# Output: ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__',
   '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__',
   '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
   '__sizeof__', '__str__', '__subclasshook__'
class Human(object):
# A simple integer data type
a = 9

# The type of a is int (i.e., a is an object of class int)
type(a) # Output: <class 'int'>

   # The type of b is float (i.e., b is an object of the class float)
   b = 9.0
   type(b) # Output: <class 'float'>

Suggestion : 3

Joined Nov 21, 2017 , Posted on Nov 29, 2017 , Joined Apr 10, 2017

code = awesome

Suggestion : 4

Class objects support two kinds of operations: attribute references and instantiation.,Now what can we do with instance objects? The only operations understood by instance objects are attribute references. There are two kinds of valid attribute names: data attributes and methods.,Attribute references use the standard syntax used for all attribute references in Python: Valid attribute names are all the names that were in the class’s namespace when the class object was created. So, if the class definition looked like this:,Generally speaking, instance variables are for data unique to each instance and class variables are for attributes and methods shared by all instances of the class:

def scope_test():
   def do_local():
   spam = "local spam"

def do_nonlocal():
   nonlocal spam
spam = "nonlocal spam"

def do_global():
   global spam
spam = "global spam"

spam = "test spam"
print("After local assignment:", spam)
print("After nonlocal assignment:", spam)
print("After global assignment:", spam)

print("In global scope:", spam)
After local assignment: test spam
After nonlocal assignment: nonlocal spam
After global assignment: nonlocal spam
In global scope: global spam
class ClassName:
class MyClass:
"A simple example class"
i = 12345

def f(self):
   return 'hello world'
x = MyClass()
def __init__(self): = []

Suggestion : 5

A Python class is created by a class definition, has an associated name space, supports attribute reference, and is callable. , An attribute defined in the class, either textually in a class definition or later by assignment to an attribute reference of the class, is a class attribute. It is stored in the class's name space (its __dict__). , Using classes to define objects provides a templating facility: class attributes and methods need only be defined once, and you can then instantiate any number of objects, all sharing the same methods. , A class is instantiated by calling the class object:

A Python class is created by a class definition, has an associated name space, supports attribute reference, and is callable.

      class name[(expr[, expr] * )]:

The simplest use of classes is as simple Cartesian product types, e.g., the records of Pascal or the structs of C.

      class foo:
         a, b, c = 0, "bar", (1, 2)

A class is instantiated by calling the class object:

      i = foo()
      print i.a, i.b, i.c

Note that new slots, which weren't defined when the class was defined, can be created at will simply by assignment. Indeed, when working interactively, an empty class definition is often handy:

      class foo: pass
      foo.a = 1
      foo.b = 2

An attribute defined in the instance, by assignment, is an instance attribute and is stored in the instance's name space -- even if there was a class attribute with the same name! Assignment via the instance results in an instance attribute that shadows the class attribute:

      class foo:
         a = 1
      i = foo()
      foo.a => 1
      i.a => 1
      i.a = "inst"
      foo.a => 1
      i.a => "inst"

Suggestion : 6

Last Updated : 26 Jul, 2022

To understand the need for creating a class in Python let’s consider an example, let’s say you wanted to track the number of dogs that may have different attributes like breed, age. If a list is used, the first element could be the dog’s breed while the second element could represent its age. Let’s suppose there are 100 different dogs, then how would you know which element is supposed to be which? What if you wanted to add other properties to these dogs? This lacks organization and it’s the exact need for classes. 

Class Definition Syntax:

   class ClassName:
   # Statement

Object Definition Syntax:
   obj = ClassName()


I 'm a mammal
I 'm a dog