Python Forum
Get name of instanciated class from class itself
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get name of instanciated class from class itself
#1
when I create an instance of a class,
I'm thinking that the new object is somewhere on the call stack,
during the instantiating process. Is it possible  to access that name without actually passing it
as part of the init process.
For example in the following routine, I want to extract the value
Bob:
class MyClass(object):
    def __init__(self, starting_index=0):
        '''
        Initialization
        :param starting_index: optional initial starting index
        '''
        self.current_index = starting_index
        # The following gives me the name of self, this class
        print(self.__class__.__name__)
        # How can I get the name of an instantiating object (Bob) here?

def testit():
    Bob = MyClass()

if __name__ == '__main__':
    testit()
Reply


Messages In This Thread
Get name of instanciated class from class itself - by Larz60+ - Nov-20-2017, 03:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Class and methods ebn852_pan 3 283 May-15-2024, 06:35 AM
Last Post: ebn852_pan
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 248 May-13-2024, 05:57 AM
Last Post: Larz60+
  Class and methods Saida2024 2 252 May-13-2024, 04:04 AM
Last Post: deanhystad
  How does this code create a class? Pedroski55 6 608 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 328 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Printing out incidence values for Class Object SquderDragon 3 373 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  class and runtime akbarza 4 474 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 638 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 812 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 859 Feb-04-2024, 09:35 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020