instantiate a class with a variable

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

    instantiate a class with a variable

    Hi, is it possible to instantiate a class with a variable.

    example

    class foo:
    def method(self):
    pass

    x='foo'

    Can I use variable x value to create an instance of my class?

    Thanks, John

  • Ben Finney

    #2
    Re: instantiate a class with a variable

    "John" <johnwadeunderw ood@yahoo.com> writes:[color=blue]
    > class foo:
    > def method(self):
    > pass
    >
    > x='foo'
    >
    > Can I use variable x value to create an instance of my class?[/color]

    You seem to be asking "is it possible to call an object whose name is
    stored in a string".

    The answer is yes::
    [color=blue][color=green][color=darkred]
    >>> class Foo:[/color][/color][/color]
    ... pass
    ...[color=blue][color=green][color=darkred]
    >>> foo_name = 'foo'
    >>> foo_class = locals().get(fo o_name)
    >>> bar = foo_class()
    >>> bar[/color][/color][/color]
    <__main__.Foo instance at 0x401e468c>

    Or, more succinctly but rather harder to follow::
    [color=blue][color=green][color=darkred]
    >>> class Foo:[/color][/color][/color]
    ... pass
    ...[color=blue][color=green][color=darkred]
    >>> foo_name = 'foo'
    >>> bar = locals().get(fo o_name)()
    >>> bar[/color][/color][/color]
    <__main__.Foo instance at 0x401e46ec>

    --
    \ "I went to the cinema, it said 'Adults: $5.00, Children $2.50'. |
    `\ So I said 'Give me two boys and a girl.'" -- Steven Wright |
    _o__) |
    Ben Finney

    Comment

    • Cameron Laird

      #3
      Re: instantiate a class with a variable

      In article <mailman.3742.1 143505181.27775 .python-list@python.org >,
      Ben Finney <bignose+hate s-spam@benfinney. id.au> wrote:[color=blue]
      >"John" <johnwadeunderw ood@yahoo.com> writes:[color=green]
      >> class foo:
      >> def method(self):
      >> pass
      >>
      >> x='foo'
      >>
      >> Can I use variable x value to create an instance of my class?[/color]
      >
      >You seem to be asking "is it possible to call an object whose name is
      >stored in a string".
      >
      >The answer is yes::
      >[color=green][color=darkred]
      > >>> class Foo:[/color][/color]
      > ... pass
      > ...[color=green][color=darkred]
      > >>> foo_name = 'foo'
      > >>> foo_class = locals().get(fo o_name)
      > >>> bar = foo_class()
      > >>> bar[/color][/color]
      > <__main__.Foo instance at 0x401e468c>
      >
      >Or, more succinctly but rather harder to follow::
      >[color=green][color=darkred]
      > >>> class Foo:[/color][/color]
      > ... pass
      > ...[color=green][color=darkred]
      > >>> foo_name = 'foo'
      > >>> bar = locals().get(fo o_name)()
      > >>> bar[/color][/color]
      > <__main__.Foo instance at 0x401e46ec>[/color]

      Comment

      • Ben Finney

        #4
        Re: instantiate a class with a variable

        Ben Finney <bignose+hate s-spam@benfinney. id.au> writes:
        [color=blue][color=green][color=darkred]
        > >>> class Foo:[/color][/color]
        > ... pass
        > ...[color=green][color=darkred]
        > >>> foo_name = 'foo'[/color][/color][/color]

        Dang. As Cameron Laird points out, this should be
        [color=blue][color=green][color=darkred]
        >>> foo_name = 'Foo'[/color][/color][/color]

        each time.

        Moral of the story: if the example already works, and then you cut and
        paste into the message compose window, and then notice that it could
        be clearer and edit it some more...

        .... always test the example code you're *actually* presenting, after
        all edits.

        --
        \ "You know what would make a good story? Something about a clown |
        `\ who makes people happy, but inside he's real sad. Also, he has |
        _o__) severe diarrhea." -- Jack Handey |
        Ben Finney

        Comment

        • bruno at modulix

          #5
          Re: instantiate a class with a variable

          John wrote:[color=blue]
          > Hi, is it possible to instantiate a class with a variable.
          >
          > example
          >
          > class foo:
          > def method(self):
          > pass
          >
          > x='foo'
          >
          > Can I use variable x value to create an instance of my class?[/color]

          You got examples using string 'foo', now if all you need is to store or
          pass around the class, just use it as any other object (yes, classes
          *are* objects):

          class Foo(object): pass

          x = Foo

          f = x()
          assert type(f) is Foo


          --
          bruno desthuilliers
          python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
          p in 'onurb@xiludom. gro'.split('@')])"

          Comment

          • John

            #6
            Re: instantiate a class with a variable

            Thanks for the help, this was exactly what I needed. Working Great
            bruno at modulix wrote:[color=blue]
            > John wrote:[color=green]
            > > Hi, is it possible to instantiate a class with a variable.
            > >
            > > example
            > >
            > > class foo:
            > > def method(self):
            > > pass
            > >
            > > x='foo'
            > >
            > > Can I use variable x value to create an instance of my class?[/color]
            >
            > You got examples using string 'foo', now if all you need is to store or
            > pass around the class, just use it as any other object (yes, classes
            > *are* objects):
            >
            > class Foo(object): pass
            >
            > x = Foo
            >
            > f = x()
            > assert type(f) is Foo
            >
            >
            > --
            > bruno desthuilliers
            > python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
            > p in 'onurb@xiludom. gro'.split('@')])"[/color]

            Comment

            Working...