Getting a class name from within main

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bg_ie@yahoo.com

    #1

    Getting a class name from within main

    Hi,

    Lets say I have the following class -

    class MyClass:
    def __init__(self):
    print (__name__.split ("."))[-1]

    if __name__ == '__main__':
    MyClassName = "MyClass"

    I can print the name of the class from within the class scope as seen
    above in the init, but is there any way of printing it from within the
    main without creating an object of the MyClass type. I need to assign
    the name of the class within my script, to a variable in main.

    Thanks,

    Barry.

  • Robin Becker

    #2
    Re: Getting a class name from within main

    bg_ie@yahoo.com wrote:
    Hi,
    >
    Lets say I have the following class -
    >
    class MyClass:
    def __init__(self):
    print (__name__.split ("."))[-1]
    >
    if __name__ == '__main__':
    MyClassName = "MyClass"
    >
    I can print the name of the class from within the class scope as seen
    above in the init, but is there any way of printing it from within the
    main without creating an object of the MyClass type. I need to assign
    the name of the class within my script, to a variable in main.
    >
    Thanks,
    >
    Barry.
    >
    >>class A:
    .... pass
    ....
    >>print A.__name__
    A
    >>>
    --
    Robin Becker

    Comment

    • Marc 'BlackJack' Rintsch

      #3
      Re: Getting a class name from within main

      In <1170838959.441 543.19720@s48g2 000cws.googlegr oups.com>, bg_ie wrote:
      class MyClass:
      def __init__(self):
      print (__name__.split ("."))[-1]
      >
      if __name__ == '__main__':
      MyClassName = "MyClass"
      >
      I can print the name of the class from within the class scope as seen
      above in the init, but is there any way of printing it from within the
      main without creating an object of the MyClass type. I need to assign
      the name of the class within my script, to a variable in main.
      Yes::

      print MyClass.__name_ _

      Ciao,
      Marc 'BlackJack' Rintsch

      Comment

      • Chris Lambacher

        #4
        Re: Getting a class name from within main

        On Wed, Feb 07, 2007 at 01:02:39AM -0800, bg_ie@yahoo.com wrote:
        Hi,
        >
        Lets say I have the following class -
        >
        class MyClass:
        def __init__(self):
        print (__name__.split ("."))[-1]
        I would spell this:
        print self.__class__. __name__
        >
        if __name__ == '__main__':
        MyClassName = "MyClass"
        >
        I can print the name of the class from within the class scope as seen
        above in the init, but is there any way of printing it from within the
        main without creating an object of the MyClass type. I need to assign
        the name of the class within my script, to a variable in main.
        >
        Thanks,
        >
        Barry.
        >
        --
        http://mail.python.org/mailman/listinfo/python-list

        Comment

        Working...