Getting a class name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Harlin Seritt

    #1

    Getting a class name

    Hi,

    How does one get the name of a class from within the class code? I
    tried something like this as a guess:

    self.__name__

    Obviously it didn't work.

    Anyone know how to do that?

    Thanks,

    Harlin

  • deelan

    #2
    Re: Getting a class name

    Harlin Seritt wrote:
    Hi,
    >
    How does one get the name of a class from within the class code? I
    tried something like this as a guess:
    >
    self.__name__
    Get the class first, then inspect its name:
    >>class Foo(object): pass
    ....
    >>f = Foo()
    >>f.__class__._ _name__
    'Foo'
    >>>
    HTH


    --
    d.

    Comment

    • goodwolf

      #3
      Re: Getting a class name

      I suppose that you wont get class name into its code (or before
      definition end) but not into a method definition.


      import sys

      def getCodeName(dea p=0):
      return sys._getframe(d eap+1).f_code.c o_name


      class MyClass (object):
      name = getCodeName() + '!'


      Comment

      • Gabriel Genellina

        #4
        Re: Getting a class name

        En Sun, 18 Feb 2007 04:20:33 -0300, goodwolf <Robert.Katic@g mail.com>
        escribió:
        I suppose that you wont get class name into its code (or before
        definition end) but not into a method definition.
        >
        >
        import sys
        >
        def getCodeName(dea p=0):
        return sys._getframe(d eap+1).f_code.c o_name
        >
        >
        class MyClass (object):
        name = getCodeName() + '!'
        What's the advantage over MyClass.__name_ _?

        --
        Gabriel Genellina

        Comment

        • Fuzzyman

          #5
          Re: Getting a class name

          On Feb 17, 8:33 pm, deelan <g...@zzz.itwro te:
          Harlin Seritt wrote:
          Hi,
          >
          How does one get the name of a class from within the class code? I
          tried something like this as a guess:
          >
          self.__name__
          >
          Get the class first, then inspect its name:
          >
          >>class Foo(object): pass
          ...
          >>f = Foo()
          >>f.__class__._ _name__
          'Foo'
          >>>
          >

          Why is the __name__ attribute not available to the instance? Why don't
          normal lookup rules apply (meaning that a magic attribute will be
          looked up on the class for instances) ?

          Fuzzyman
          http://www.voidspace.org.uk/python/articles.shtml

          HTH
          >
          --
          d.

          Comment

          • Daniel Klein

            #6
            Re: Getting a class name

            On 18 Feb 2007 04:24:47 -0800, "Fuzzyman" <fuzzyman@gmail .comwrote:
            >On Feb 17, 8:33 pm, deelan <g...@zzz.itwro te:
            >Harlin Seritt wrote:
            Hi,
            >>
            How does one get the name of a class from within the class code? I
            tried something like this as a guess:
            >>
            self.__name__
            >>
            >Get the class first, then inspect its name:
            >>
            > >>class Foo(object): pass
            >...
            > >>f = Foo()
            > >>f.__class__._ _name__
            >'Foo'
            > >>>
            >>
            >
            >
            >Why is the __name__ attribute not available to the instance? Why don't
            >normal lookup rules apply (meaning that a magic attribute will be
            >looked up on the class for instances) ?
            Good question!
            >>f = Foo()
            >>dir(f)
            ['__class__', '__delattr__', '__dict__', '__doc__',
            '__getattribute __', '__hash__', '__init__', '__module__', '__new__',
            '__reduce__', '__reduce_ex__' , '__repr__', '__setattr__', '__str__',
            '__weakref__']
            >>dir(f.__class __)
            ['__class__', '__delattr__', '__dict__', '__doc__',
            '__getattribute __', '__hash__', '__init__', '__module__', '__new__',
            '__reduce__', '__reduce_ex__' , '__repr__', '__setattr__', '__str__',
            '__weakref__']
            >>>
            Where is '__name__' ?


            Dan

            Comment

            • Michele Simionato

              #7
              Re: Getting a class name

              On Feb 18, 1:24 pm, "Fuzzyman" <fuzzy...@gmail .comwrote:
              Why is the __name__ attribute not available to the instance? Why don't
              normal lookup rules apply (meaning that a magic attribute will be
              looked up on the class for instances) ?
              Because __name__ isn't really an attribute, it is a descriptor defined
              on
              the metaclass:
              >>type(type.__d ict__['__name__'])
              <type 'getset_descrip tor'>

              See http://users.rcn.com/python/download/Descriptor.htm for a guide to
              descriptors, and the papers by me and David Mertz for a guide to
              metaclasses.

              Michele Simionato

              Comment

              • Fuzzyman

                #8
                Re: Getting a class name

                On Feb 18, 1:22 pm, "Michele Simionato" <michele.simion ...@gmail.com>
                wrote:
                On Feb 18, 1:24 pm, "Fuzzyman" <fuzzy...@gmail .comwrote:
                >
                Why is the __name__ attribute not available to the instance? Why don't
                normal lookup rules apply (meaning that a magic attribute will be
                looked up on the class for instances) ?
                >
                Because __name__ isn't really an attribute, it is a descriptor defined
                on
                the metaclass:
                >
                >type(type.__di ct__['__name__'])
                >
                <type 'getset_descrip tor'>
                >
                Seehttp://users.rcn.com/python/download/Descriptor.htmf or a guide to
                descriptors, and the papers by me and David Mertz for a guide to
                metaclasses.
                >
                Thanks, I'll do some more reading.

                I got as far as this on my own:

                My guess is that it is because magic attributes are looked up on the
                class. When you ask the *class* what its name is, the metaclass
                answers on its behalf.

                The class has no real ``__name__`` attribute, so when you ask the
                instance (which doesn't inherit from the metaclass) you get an
                ``AttributeErro r``. Is this right ? In which case, how does the
                metaclass (typically ``type`` which has many instance) know which
                answer to supply ?

                A simple test indicates that the name *is* looked up on the
                metaclass :

                ... raw:: html

                {+coloring}
                >>class meta(type):
                ... __name__ = 'fish'
                ...
                >>class Test(object):
                ... __metaclass__ = meta
                ...
                >>Test.__name __
                'fish'
                {-coloring}

                So maybe ``__name__`` is a propery and ``type`` keeps a dictionary of
                instances to names, registered in ``type.__new__` ` ?

                Fuzzyman
                http://www.voidspace.org.uk/python/articles.shtml
                Michele Simionato

                Comment

                • goodwolf

                  #9
                  Re: Getting a class name

                  On Feb 18, 9:17 am, "Gabriel Genellina" <gagsl...@yahoo .com.arwrote:
                  En Sun, 18 Feb 2007 04:20:33 -0300, goodwolf <Robert.Ka...@g mail.com>
                  escribió:
                  >
                  I suppose that you wont get class name into its code (or before
                  definition end) but not into a method definition.
                  >
                  import sys
                  >
                  def getCodeName(dea p=0):
                  return sys._getframe(d eap+1).f_code.c o_name
                  >
                  class MyClass (object):
                  name = getCodeName() + '!'
                  >
                  What's the advantage over MyClass.__name_ _?
                  >
                  --
                  Gabriel Genellina
                  >>class C(object):
                  .... name = C.__name__
                  ....
                  Traceback (most recent call last):
                  File "<stdin>", line 1, in ?
                  File "<stdin>", line 2, in C
                  NameError: name 'C' is not defined
                  >>>

                  Comment

                  • Gabriel Genellina

                    #10
                    Re: Getting a class name

                    En Sun, 18 Feb 2007 14:14:41 -0300, goodwolf <Robert.Katic@g mail.com>
                    escribió:
                    On Feb 18, 9:17 am, "Gabriel Genellina" <gagsl...@yahoo .com.arwrote:
                    >En Sun, 18 Feb 2007 04:20:33 -0300, goodwolf <Robert.Ka...@g mail.com>
                    >escribió:
                    >>
                    I suppose that you wont get class name into its code (or before
                    definition end) but not into a method definition.
                    >>
                    import sys
                    >>
                    def getCodeName(dea p=0):
                    return sys._getframe(d eap+1).f_code.c o_name
                    >>
                    class MyClass (object):
                    name = getCodeName() + '!'
                    >>
                    >What's the advantage over MyClass.__name_ _?
                    >>
                    >--
                    >Gabriel Genellina
                    >
                    >>>class C(object):
                    ... name = C.__name__
                    ...
                    Traceback (most recent call last):
                    File "<stdin>", line 1, in ?
                    File "<stdin>", line 2, in C
                    NameError: name 'C' is not defined
                    >>>>
                    I were asking, why do you want a "name" attribute since "__name__" already
                    exists and has the needed information. And worst, using an internal
                    implementation function to do such task.

                    --
                    Gabriel Genellina

                    Comment

                    • Bruno Desthuilliers

                      #11
                      Re: Getting a class name

                      Gabriel Genellina a écrit :
                      En Sun, 18 Feb 2007 14:14:41 -0300, goodwolf <Robert.Katic@g mail.com>
                      escribió:
                      >
                      >On Feb 18, 9:17 am, "Gabriel Genellina" <gagsl...@yahoo .com.arwrote:
                      >>
                      >>En Sun, 18 Feb 2007 04:20:33 -0300, goodwolf <Robert.Ka...@g mail.com>
                      >>escribió:
                      >>>
                      >I suppose that you wont get class name into its code (or before
                      >definition end) but not into a method definition.
                      >>>
                      >import sys
                      >>>
                      >def getCodeName(dea p=0):
                      > return sys._getframe(d eap+1).f_code.c o_name
                      >>>
                      >class MyClass (object):
                      > name = getCodeName() + '!'
                      >>>
                      >>What's the advantage over MyClass.__name_ _?
                      >>>
                      >>--
                      >>Gabriel Genellina
                      >>
                      >>
                      >>>>class C(object):
                      >>
                      >... name = C.__name__
                      >...
                      >Traceback (most recent call last):
                      > File "<stdin>", line 1, in ?
                      > File "<stdin>", line 2, in C
                      >NameError: name 'C' is not defined
                      >>
                      >>>>>
                      >
                      I were asking, why do you want a "name" attribute since "__name__"
                      already exists and has the needed information. And worst, using an
                      internal implementation function to do such task.
                      >
                      This might be useful to avoid metaclass hacks when trying to initialize
                      a class attribute that would require the class name. (my 2 cents)

                      Comment

                      • Fuzzyman

                        #12
                        Re: Getting a class name

                        On Feb 18, 11:54 pm, Bruno Desthuilliers
                        <bdesth.quelque ch...@free.quel quepart.frwrote :
                        Gabriel Genellina a écrit :
                        >
                        En Sun, 18 Feb 2007 14:14:41 -0300, goodwolf <Robert.Ka...@g mail.com>
                        escribió:
                        >
                        On Feb 18, 9:17 am, "Gabriel Genellina" <gagsl...@yahoo .com.arwrote:
                        >
                        >En Sun, 18 Feb 2007 04:20:33 -0300, goodwolf <Robert.Ka...@g mail.com>
                        >escribió:
                        >
                        I suppose that you wont get class name into its code (or before
                        definition end) but not into a method definition.
                        >
                        import sys
                        >
                        def getCodeName(dea p=0):
                        return sys._getframe(d eap+1).f_code.c o_name
                        >
                        class MyClass (object):
                        name = getCodeName() + '!'
                        >
                        >What's the advantage over MyClass.__name_ _?
                        >
                        >--
                        >Gabriel Genellina
                        >
                        >>>class C(object):
                        >
                        ... name = C.__name__
                        ...
                        Traceback (most recent call last):
                        File "<stdin>", line 1, in ?
                        File "<stdin>", line 2, in C
                        NameError: name 'C' is not defined
                        >
                        I were asking, why do you want a "name" attribute since "__name__"
                        already exists and has the needed information. And worst, using an
                        internal implementation function to do such task.
                        >
                        This might be useful to avoid metaclass hacks when trying to initialize
                        a class attribute that would require the class name. (my 2 cents)
                        It's still an ugly hack. :-)

                        (But a nice implementation detail to know about none-the-less.)

                        Fuzzyman
                        http://www.voidspace.org.uk/python/articles.shtml

                        Comment

                        • Gabriel Genellina

                          #13
                          Re: Getting a class name

                          En Sun, 18 Feb 2007 20:56:48 -0300, Fuzzyman <fuzzyman@gmail .comescribió:
                          [somebody] wrote:
                          >def getCodeName(dea p=0):
                          > return sys._getframe(d eap+1).f_code.c o_name
                          >>
                          >class MyClass (object):
                          > name = getCodeName() + '!'
                          >>
                          >>What's the advantage over MyClass.__name_ _?
                          I were asking, why do you want a "name" attribute since "__name__"
                          already exists and has the needed information. And worst, using an
                          internal implementation function to do such task.
                          >>
                          >This might be useful to avoid metaclass hacks when trying to initialize
                          >a class attribute that would require the class name. (my 2 cents)
                          >
                          It's still an ugly hack. :-)
                          (But a nice implementation detail to know about none-the-less.)
                          Having class decorators would be nice...

                          --
                          Gabriel Genellina

                          Comment

                          • Fuzzyman

                            #14
                            Re: Getting a class name

                            On Feb 19, 5:11 am, "Gabriel Genellina" <gagsl...@yahoo .com.arwrote:
                            En Sun, 18 Feb 2007 20:56:48 -0300, Fuzzyman <fuzzy...@gmail .comescribió:
                            >
                            >
                            >
                            [somebody] wrote:
                            def getCodeName(dea p=0):
                            return sys._getframe(d eap+1).f_code.c o_name
                            >
                            class MyClass (object):
                            name = getCodeName() + '!'
                            >
                            >What's the advantage over MyClass.__name_ _?
                            I were asking, why do you want a "name" attribute since "__name__"
                            already exists and has the needed information. And worst, using an
                            internal implementation function to do such task.
                            >
                            This might be useful to avoid metaclass hacks when trying to initialize
                            a class attribute that would require the class name. (my 2 cents)
                            >
                            It's still an ugly hack. :-)
                            (But a nice implementation detail to know about none-the-less.)
                            >
                            Having class decorators would be nice...
                            >
                            Yes - and Guido said he wouldn't be opposed to the idea when it was
                            poitned out that they would be very useful for both IronPython and
                            Jython to implement features of their underlying platforms.

                            I haven't heard anyone (other than you) mention them recently
                            though...

                            Fuzzyman
                            http://www.voidspace.org.uk/python/articles.shtml
                            --
                            Gabriel Genellina

                            Comment

                            • Gabriel Genellina

                              #15
                              Re: Getting a class name

                              En Mon, 19 Feb 2007 17:25:56 -0300, Fuzzyman <fuzzyman@gmail .comescribió:
                              On Feb 19, 5:11 am, "Gabriel Genellina" <gagsl...@yahoo .com.arwrote:
                              >En Sun, 18 Feb 2007 20:56:48 -0300, Fuzzyman <fuzzy...@gmail .com>
                              >escribió:
                              [somebody] wrote:
                              >def getCodeName(dea p=0):
                              > return sys._getframe(d eap+1).f_code.c o_name
                              >>
                              >This might be useful to avoid metaclass hacks when trying to
                              >initialize
                              >a class attribute that would require the class name. (my 2 cents)
                              >>
                              It's still an ugly hack. :-)
                              (But a nice implementation detail to know about none-the-less.)
                              >>
                              >Having class decorators would be nice...
                              >
                              Yes - and Guido said he wouldn't be opposed to the idea when it was
                              poitned out that they would be very useful for both IronPython and
                              Jython to implement features of their underlying platforms.
                              >
                              I haven't heard anyone (other than you) mention them recently
                              though...
                              Maybe it's a sign that actually nobody cares about class decorators :(

                              --
                              Gabriel Genellina

                              Comment

                              Working...