Variable Variable

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

    Variable Variable

    Hi List, is there in python a variable variable like in PHP ($$var)?

    What I want to do is something like that:

    pc=["a","b","c"]

    for i in pc:
    i = anyclass()

    a.shutdown()
    b.update()


    Any Ideas?

    Many Thanks, m
  • Leif K-Brooks

    #2
    Re: Variable Variable

    Tanteauguri wrote:[color=blue]
    > Hi List, is there in python a variable variable like in PHP ($$var)?
    >
    > What I want to do is something like that:
    >
    > pc=["a","b","c"]
    >
    > for i in pc:
    > i = anyclass()
    >
    > a.shutdown()
    > b.update()[/color]

    Use a dictionary:

    stuff = {}
    pc = ['a', 'b', 'c']

    for i in pc:
    stuff[i] = anyclass()

    stuff['a'].shutdown()
    stuff['b'].update()

    Comment

    • Premshree Pillai

      #3
      Re: Variable Variable

      On Sat, 19 Mar 2005 04:35:47 -0500, Leif K-Brooks <eurleif@ecritt ers.biz> wrote:[color=blue]
      > Tanteauguri wrote:[color=green]
      > > Hi List, is there in python a variable variable like in PHP ($$var)?
      > >
      > > What I want to do is something like that:
      > >
      > > pc=["a","b","c"]
      > >
      > > for i in pc:
      > > i = anyclass()
      > >
      > > a.shutdown()
      > > b.update()[/color]
      >
      > Use a dictionary:
      >
      > stuff = {}
      > pc = ['a', 'b', 'c'][/color]

      I think what he wants to do is basically hold object names in a tuple
      list, and then call the methods on those objects (while iterating or
      whatever).
      [color=blue]
      >
      > for i in pc:
      > stuff[i] = anyclass()
      >
      > stuff['a'].shutdown()
      > stuff['b'].update()
      > --
      > http://mail.python.org/mailman/listinfo/python-list
      >[/color]


      --
      Premshree Pillai

      Comment

      • Kay Schluehr

        #4
        Re: Variable Variable


        Tanteauguri wrote:[color=blue]
        > Hi List, is there in python a variable variable like in PHP ($$var)?
        >
        > What I want to do is something like that:
        >
        > pc=["a","b","c"]
        >
        > for i in pc:
        > i = anyclass()
        >
        > a.shutdown()
        > b.update()
        >
        >
        > Any Ideas?[/color]

        def seq(n,cls,*args ,**kw):
        "create a sequence of n objects of type cls."
        return [cls(*args,**kw) for i in range(n)]
        [color=blue][color=green][color=darkred]
        >>> a,b,c = seq(3,anyclass)
        >>> a.shutdown()
        >>> b.update()[/color][/color][/color]

        Regards Kay

        Comment

        • Cameron Laird

          #5
          Re: Variable Variable

          In article <1111229201.840 663.141530@g14g 2000cwa.googleg roups.com>,
          Kay Schluehr <kay.schluehr@g mx.net> wrote:[color=blue]
          >
          >Tanteauguri wrote:[color=green]
          >> Hi List, is there in python a variable variable like in PHP ($$var)?
          >>
          >> What I want to do is something like that:
          >>
          >> pc=["a","b","c"]
          >>
          >> for i in pc:
          >> i = anyclass()
          >>
          >> a.shutdown()
          >> b.update()
          >>
          >>
          >> Any Ideas?[/color]
          >
          >def seq(n,cls,*args ,**kw):
          > "create a sequence of n objects of type cls."
          > return [cls(*args,**kw) for i in range(n)]
          >[color=green][color=darkred]
          >>>> a,b,c = seq(3,anyclass)
          >>>> a.shutdown()
          >>>> b.update()[/color][/color]
          >
          >Regards Kay
          >[/color]

          I'm going to make this explicit: the PHP idiom is a defect.
          Yes, I know that's good style among top PHP practitioners,
          but, from all I know, it's simply a bad habit. The advice to
          use a dictionary is on target.

          Comment

          • Bruno Desthuilliers

            #6
            Re: Variable Variable

            Tanteauguri a écrit :[color=blue]
            > Hi List, is there in python a variable variable like in PHP ($$var)?[/color]

            Hopefully, no.

            See other answers in that thread for pythonic idioms.

            Comment

            • Kay Schluehr

              #7
              Re: Variable Variable

              > I'm going to make this explicit: the PHP idiom is a defect.[color=blue]
              > Yes, I know that's good style among top PHP practitioners,
              > but, from all I know, it's simply a bad habit. The advice to
              > use a dictionary is on target.[/color]

              I tried to like it the whole last hour ;-)

              I reconstructed the formal structure of $$var from a PHP Web-tutorial
              though i do not have any experience with the language.

              This is what i got:

              $ =
              <Name> ---> <Var> --------------> <Var>x<String >
              | |
              $ | # | $ o q' o proj2
              | |
              V V
              <Var> ----- f ------> <Var>
              | |
              | # | =
              | V
              +-----------------> <Var>x<String >
              =

              In the diagram $ is an operator that creates a var from a name, = is an
              operator that binds a string to a var and q' drops quotations from a
              string to get an name. The function f is an implied isomorphism. It
              represents the equivalence of $$a and $b, where $a = "b".

              Personally I could live with that, but the diagram is a bit special
              because of the restriction of the = operation. I do not know if PHP
              supports this operational view by enabling iterations: $a, $$a, $$$a
              .... ?

              After all I can also live without that in Python...

              Regards Kay

              Comment

              • Diez B. Roggisch

                #8
                Re: Variable Variable

                >[color=blue]
                > Personally I could live with that, but the diagram is a bit special
                > because of the restriction of the = operation. I do not know if PHP
                > supports this operational view by enabling iterations: $a, $$a, $$$a
                > ... ?[/color]

                It does.
                --
                Regards,

                Diez B. Roggisch

                Comment

                • MikeyG

                  #9
                  Re: Variable Variable

                  Premshree Pillai wrote:[color=blue]
                  > On Sat, 19 Mar 2005 04:35:47 -0500, Leif K-Brooks <eurleif@ecritt ers.biz> wrote:
                  >[color=green]
                  >>Tanteauguri wrote:
                  >>[color=darkred]
                  >>>Hi List, is there in python a variable variable like in PHP ($$var)?
                  >>>
                  >>>What I want to do is something like that:
                  >>>
                  >>>pc=["a","b","c"]
                  >>>
                  >>>for i in pc:
                  >>> i = anyclass()
                  >>>
                  >>>a.shutdown ()
                  >>>b.update()[/color]
                  >>
                  >>Use a dictionary:
                  >>
                  >>stuff = {}
                  >>pc = ['a', 'b', 'c'][/color]
                  >
                  >
                  > I think what he wants to do is basically hold object names in a tuple
                  > list, and then call the methods on those objects (while iterating or
                  > whatever).
                  >
                  >[color=green]
                  >>for i in pc:
                  >> stuff[i] = anyclass()
                  >>
                  >>stuff['a'].shutdown()
                  >>stuff['b'].update()
                  >>--
                  >>http://mail.python.org/mailman/listinfo/python-list
                  >>[/color]
                  >[/color]

                  Try this:

                  pc=["a","b","c"]
                  for i in pc:
                  vars()[i] = anyclass()
                  a.shutdown()
                  b.update()

                  MikeG

                  Comment

                  • Mike Gould

                    #10
                    Re: Variable Variable

                    Premshree Pillai wrote:[color=blue]
                    > On Sat, 19 Mar 2005 04:35:47 -0500, Leif K-Brooks <eurleif@ecritt ers.biz> wrote:
                    >[color=green]
                    >>Tanteauguri wrote:
                    >>[color=darkred]
                    >>>Hi List, is there in python a variable variable like in PHP ($$var)?
                    >>>
                    >>>What I want to do is something like that:
                    >>>
                    >>>pc=["a","b","c"]
                    >>>
                    >>>for i in pc:
                    >>> i = anyclass()
                    >>>
                    >>>a.shutdown ()
                    >>>b.update()[/color]
                    >>
                    >>Use a dictionary:
                    >>
                    >>stuff = {}
                    >>pc = ['a', 'b', 'c'][/color]
                    >
                    >
                    > I think what he wants to do is basically hold object names in a tuple
                    > list, and then call the methods on those objects (while iterating or
                    > whatever).
                    >
                    >[color=green]
                    >>for i in pc:
                    >> stuff[i] = anyclass()
                    >>
                    >>stuff['a'].shutdown()
                    >>stuff['b'].update()
                    >>--
                    >>http://mail.python.org/mailman/listinfo/python-list
                    >>[/color]
                    >[/color]

                    Try this:

                    pc=["a","b","c"]
                    for i in pc:
                    vars()[i] = anyclass()
                    a.shutdown()
                    b.update()

                    MikeG

                    Comment

                    • Mike Gould

                      #11
                      Re: Variable Variable

                      Premshree Pillai wrote:[color=blue]
                      > On Sat, 19 Mar 2005 04:35:47 -0500, Leif K-Brooks <eurleif@ecritt ers.biz> wrote:
                      >[color=green]
                      >>Tanteauguri wrote:
                      >>[color=darkred]
                      >>>Hi List, is there in python a variable variable like in PHP ($$var)?
                      >>>
                      >>>What I want to do is something like that:
                      >>>
                      >>>pc=["a","b","c"]
                      >>>
                      >>>for i in pc:
                      >>> i = anyclass()
                      >>>
                      >>>a.shutdown ()
                      >>>b.update()[/color]
                      >>
                      >>Use a dictionary:
                      >>
                      >>stuff = {}
                      >>pc = ['a', 'b', 'c'][/color]
                      >
                      >
                      > I think what he wants to do is basically hold object names in a tuple
                      > list, and then call the methods on those objects (while iterating or
                      > whatever).
                      >
                      >[color=green]
                      >>for i in pc:
                      >> stuff[i] = anyclass()
                      >>
                      >>stuff['a'].shutdown()
                      >>stuff['b'].update()
                      >>--
                      >>http://mail.python.org/mailman/listinfo/python-list
                      >>[/color]
                      >[/color]

                      Try this:

                      pc=["a","b","c"]
                      for i in pc:
                      vars()[i] = anyclass()
                      a.shutdown()
                      b.update()

                      MikeG


                      Comment

                      Working...