introspection inquiry

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

    #1

    introspection inquiry

    Where in the language would one find the intropsection capability to
    answer the question: what class am I in?

    Example:

    class ExistentialCris is:
    def __init__(self, text):
    self.spam = text
    print 'In the constructor of the %s class' % <whatever>

    When the constructor method is invoked, would like to see the following
    message:

    In the constructor of the ExistentialCris is class

    and I would like to be able to do it without substituting the literal
    string 'ExistentialCri sis' for <whatever>.

    My question is this: what can be substituted for <whatever> that will
    make the example above work?

  • Robin Becker

    #2
    Re: introspection inquiry

    mirandacascade@ yahoo.com wrote:
    ....[color=blue]
    >
    > My question is this: what can be substituted for <whatever> that will
    > make the example above work?
    >[/color]

    self.__class__. __name__
    --
    Robin Becker

    Comment

    • Michael Hoffman

      #3
      Re: introspection inquiry

      Robin Becker wrote:[color=blue]
      > self.__class__. __name__[/color]

      Unless I misunderstood the question, that won't work. That will
      give you the name of the class the object is an instance is of.
      I think he wants the name of the class the method was defined in.

      Here's a way to do that using metaclasses and Python's magic
      double-underscore attribute-mangling feature:

      """
      class SelfKnowledge(t ype):
      def __init__(cls, name, bases, dict):
      setattr(cls, "_%s__%s" % (name, "class_name "), name)
      type.__init__(c ls, name, bases, dict)

      class Nietzsche(objec t):
      __metaclass__ = SelfKnowledge

      def __init__(self, text):
      self.spam = text
      print "In the constructor of the %s class" % self.__class_na me

      class Kierkegaard(Nie tzsche):
      def __init__(self, text):
      print "Now in the constructor of %s" % self.__class_na me
      Nietzsche.__ini t__(self, text)

      Nietzsche("Thus Spake Zarathustra")
      print

      Kierkegaard("Fe ar and Trembling")
      """

      $ python test1.py
      In the constructor of the Nietzsche class

      Now in the constructor of Kierkegaard
      In the constructor of the Nietzsche class
      --
      Michael Hoffman

      Comment

      • Diez B. Roggisch

        #4
        Re: introspection inquiry

        > Unless I misunderstood the question, that won't work. That will[color=blue]
        > give you the name of the class the object is an instance is of.
        > I think he wants the name of the class the method was defined in.[/color]

        Where is the difference? The method is defined in a class - and an instance
        is created from that class.

        This works as expected:

        class ExistentialCris is:
        def __init__(self, text):
        self.spam = text
        print 'In the constructor of the %s class' % self.__class__. __name__


        ExistentialCris is("egal")


        --
        Regards,

        Diez B. Roggisch

        Comment

        • John Roth

          #5
          Re: introspection inquiry


          "Michael Hoffman" <cam.ac.uk@mh39 1.invalid> wrote in message
          news:cvaaae$77q $1@gemini.csx.c am.ac.uk...[color=blue]
          > Robin Becker wrote:[color=green]
          >> self.__class__. __name__[/color]
          >
          > Unless I misunderstood the question, that won't work. That will
          > give you the name of the class the object is an instance is of.
          > I think he wants the name of the class the method was defined in.[/color]

          If that's the case, then the inspect module should give
          the tools to do it without a great deal of angst. Unfortunately,
          it doesn't give you the class that defined the method, just
          the class that invoked it.

          John Roth


          Comment

          • Michael Hoffman

            #6
            Re: introspection inquiry

            Diez B. Roggisch wrote:[color=blue]
            >[Michael Hoffman]:[color=green]
            >>Unless I misunderstood the question, that won't work. That will
            >>give you the name of the class the object is an instance is of.
            >>I think he wants the name of the class the method was defined in.[/color]
            >
            > Where is the difference? The method is defined in a class - and an instance
            > is created from that class.
            >
            > This works as expected:
            >
            > class ExistentialCris is:
            > def __init__(self, text):
            > self.spam = text
            > print 'In the constructor of the %s class' % self.__class__. __name__
            >
            >
            > ExistentialCris is("egal")[/color]

            Yes, but this doesn't work if you have a subclass:

            """
            class ExistentialCris isSubclass(Exis tentialCrisis):
            def __init__(self, text):
            print "New constructor"
            ExistentialCris is.__init__(sel f, text)

            ExistentialCris isSubclass("who a")
            """

            gives you:

            New constructor
            In the constructor of the ExistentialCris isSubclass class

            But the second line is *not* in the constructor of
            ExistentialCris isSubclass, it is in the constructor of ExistentialCris is.

            while I read the original post as saying that he wanted
            "ExistentialCri sis" there instead. Indeed this example

            --
            Michael Hoffman

            Comment

            • Michael Hoffman

              #7
              Re: introspection inquiry

              John Roth wrote:
              [color=blue]
              > If that's the case, then the inspect module should give
              > the tools to do it without a great deal of angst. Unfortunately,
              > it doesn't give you the class that defined the method, just
              > the class that invoked it.[/color]

              Are you saying that the inspect module *should* give you the
              tools to do it but does not?
              --
              Michael Hoffman

              Comment

              • Robin Becker

                #8
                Re: introspection inquiry

                Michael Hoffman wrote:[color=blue]
                > Robin Becker wrote:
                >[color=green]
                >> self.__class__. __name__[/color]
                >
                >
                > Unless I misunderstood the question, that won't work. That will
                > give you the name of the class the object is an instance is of.
                > I think he wants the name of the class the method was defined in.
                >
                > Here's a way to do that using metaclasses and Python's magic
                > double-underscore attribute-mangling feature:
                >
                > """
                > class SelfKnowledge(t ype):
                > def __init__(cls, name, bases, dict):
                > setattr(cls, "_%s__%s" % (name, "class_name "), name)
                > type.__init__(c ls, name, bases, dict)
                >
                > class Nietzsche(objec t):
                > __metaclass__ = SelfKnowledge
                >
                > def __init__(self, text):
                > self.spam = text
                > print "In the constructor of the %s class" % self.__class_na me
                >
                > class Kierkegaard(Nie tzsche):
                > def __init__(self, text):
                > print "Now in the constructor of %s" % self.__class_na me
                > Nietzsche.__ini t__(self, text)
                >
                > Nietzsche("Thus Spake Zarathustra")
                > print
                >
                > Kierkegaard("Fe ar and Trembling")
                > """
                >
                > $ python test1.py
                > In the constructor of the Nietzsche class
                >
                > Now in the constructor of Kierkegaard
                > In the constructor of the Nietzsche class[/color]

                I guess if you're right something along the lines of
                import inspect
                class A:
                _class_name=ins pect.currentfra me().f_code.co_ name
                def __init__(self,t ext,_defining_c lass_name=_clas s_name):
                print 'text=',text,'_ defining_class_ name=',_definin g_class_name

                class B(A):
                pass
                b=B('aaa')

                ==>text= aaa _defining_class _name= A


                could work as well, but if we only need the local name why not just
                insert directly.
                --
                Robin Becker

                Comment

                • Michael Hoffman

                  #9
                  Re: introspection inquiry

                  Robin Becker wrote:[color=blue]
                  > import inspect
                  > class A:
                  > _class_name=ins pect.currentfra me().f_code.co_ name
                  > def __init__(self,t ext,_defining_c lass_name=_clas s_name):
                  > print 'text=',text,'_ defining_class_ name=',_definin g_class_name
                  >
                  > class B(A):
                  > pass
                  > b=B('aaa')[/color]

                  That won't work, if you, say, wanted to print out the name of the
                  class the constructor was defined in for a whole chain of
                  constructors. Which is about the only case I can think of where
                  this would be useful.
                  [color=blue]
                  > could work as well, but if we only need the local name why not just
                  > insert directly.[/color]

                  To be honest, that is what I have done every time I have needed
                  something like this.

                  I've only used metaclasses in production code once and I still debate
                  whether that case is a good idea or not.
                  --
                  Michael Hoffman

                  Comment

                  Working...