How to create an object instance from a string??

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

    How to create an object instance from a string??

    How can I create an instance of an object from a string?

    For example, I have a class Dog:
    class Dog:
    def bark(self):
    print "Arf!!!"


    I have a string:
    classname = "Dog"

    How can I create a instance of Dog from classname?
    Is there any such methods like those in Java?

  • Patrick Useldinger

    #2
    Re: How to create an object instance from a string??

    Tian wrote:
    [color=blue]
    > I have a string:
    > classname = "Dog"[/color]

    It's easier without strings:
    [color=blue][color=green][color=darkred]
    >>> classname = Dog
    >>> classname().bar k()[/color][/color][/color]
    Arf!!![color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Comment

    • Ksenia Marasanova

      #3
      Re: How to create an object instance from a string??

      On 19 Mar 2005 14:16:42 -0800, Tian <wangtianthu@gm ail.com> wrote:[color=blue]
      > How can I create an instance of an object from a string?
      >
      > For example, I have a class Dog:
      > class Dog:
      > def bark(self):
      > print "Arf!!!"
      >
      > I have a string:
      > classname = "Dog"
      >
      > How can I create a instance of Dog from classname?[/color]

      If class Dog is in the same namespace:
      dog_class = globals()[classname]
      dog = dog_class()
      dog.bark()

      If class is declared in another file, e.g. "animals.py ":
      import animals
      dog_class = getattr(animals , classname)
      dog = dog_class()
      dog.bark()


      --
      Ksenia

      Comment

      • Peter Hansen

        #4
        Re: How to create an object instance from a string??

        Tian wrote:[color=blue]
        > How can I create an instance of an object from a string?
        >
        > For example, I have a class Dog:
        > class Dog:
        > def bark(self):
        > print "Arf!!!"
        >
        >
        > I have a string:
        > classname = "Dog"
        >
        > How can I create a instance of Dog from classname?
        > Is there any such methods like those in Java?[/color]

        You generally get a reference to the class object
        (i.e. to Dog) using either globals(), locals(),
        or getattr() as appropriate for the specific
        situation at hand...

        -Peter

        Comment

        • Tian

          #5
          Re: How to create an object instance from a string??

          This is very useful, thanks!

          Comment

          • Do Re Mi chel La Si Do

            #6
            Re: How to create an object instance from a string??

            Hi !


            Also :


            classname = "Dog"
            exec("b="+class name+"()")
            b.bark()

            or

            classname = "Dog"
            exec("cl="+clas sname)
            b=cl()
            b.bark()



            Michel Claveau




            Comment

            • Peter Hansen

              #7
              Re: How to create an object instance from a string??

              Do Re Mi chel La Si Do wrote:[color=blue]
              > Also :
              > classname = "Dog"
              > exec("b="+class name+"()")
              > b.bark()
              >
              > or
              > classname = "Dog"
              > exec("cl="+clas sname)
              > b=cl()
              > b.bark()[/color]

              Ugh.

              The statement exec (note: it's a statement, not a
              function call as you imply with the above) is rarely
              either required or good style.

              In this case it's neither.

              -Peter

              Comment

              • Do Re Mi chel La Si Do

                #8
                Re: How to create an object instance from a string??

                Hi !


                Sorry, but :

                The statement exec does not have impact on the style.

                Like with exefile or import (and other), the style is determined by the
                contents. Rather judge "exec" (or import, or execfile) than the contents,
                it is an error of causality.


                @-salutations
                --
                Michel Claveau





                Comment

                • Max M

                  #9
                  Re: How to create an object instance from a string??

                  Tian wrote:[color=blue]
                  > How can I create an instance of an object from a string?
                  >
                  > For example, I have a class Dog:[/color]


                  class Dog:
                  def bark(self):
                  print "Arf!!!"

                  def Factory(class_n ame):
                  classes = {
                  'Dog':Dog
                  }
                  return classes[class_name]


                  dog = Factory('Dog')( )


                  --

                  hilsen/regards Max M, Denmark


                  IT's Mad Science

                  Comment

                  • Do Re Mi chel La Si Do

                    #10
                    Re: How to create an object instance from a string??

                    Hi !

                    Finally, it's the best solution.

                    Michel Claveau


                    Comment

                    Working...