"configuring" a class

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

    #1

    "configuring" a class

    I would like to let the user of one of my classes "configure" it by
    activating or de-activating a particular behavior (for all instances of
    the class).

    One way to do this, I figured, is to have a static class variable,
    along with a method to set the variable. However, I am stumped as to
    how to do that in python. Suggestions welcome.

    The next best thing, I figure, is to just use a global variable.
    Several "methods" of the class then check the value of the global
    variable to determine what to do. The user can then just set the
    variable to get the desired behavior.

    However, I tried this and it does not seem to work. I imported the
    class, then set the global variable. But the new value of the variable
    somehow did not get back into the class methods that need to see it.

    Can anyone give me a clue about this? If there is a better way, please
    let me know. Thanks.

  • James Stroud

    #2
    Re: "configuri ng" a class

    Russ wrote:[color=blue]
    > I would like to let the user of one of my classes "configure" it by
    > activating or de-activating a particular behavior (for all instances of
    > the class).
    >
    > One way to do this, I figured, is to have a static class variable,
    > along with a method to set the variable. However, I am stumped as to
    > how to do that in python. Suggestions welcome.
    >
    > The next best thing, I figure, is to just use a global variable.
    > Several "methods" of the class then check the value of the global
    > variable to determine what to do. The user can then just set the
    > variable to get the desired behavior.
    >
    > However, I tried this and it does not seem to work. I imported the
    > class, then set the global variable. But the new value of the variable
    > somehow did not get back into the class methods that need to see it.
    >
    > Can anyone give me a clue about this? If there is a better way, please
    > let me know. Thanks.
    >[/color]

    py> class C:
    .... doit = 2
    ....
    py> C.doit
    2
    py> a = C()
    py> b = C()
    py> c = C()
    py> a.doit, b.doit, c.doit
    (2, 2, 2)
    py> C.doit = 5
    py> a.doit, b.doit, c.doit
    (5, 5, 5)
    py> b.doit = 13
    py> a.doit, b.doit, c.doit
    (5, 13, 5)

    James

    Comment

    • Terry Hancock

      #3
      Re: "configuri ng" a class

      On 22 Feb 2006 17:28:35 -0800
      "Russ" <uymqlp502@snea kemail.com> wrote:[color=blue]
      > I would like to let the user of one of my classes
      > "configure" it by activating or de-activating a particular
      > behavior (for all instances of the class).[/color]
      [color=blue]
      > One way to do this, I figured, is to have a static class
      > variable, along with a method to set the variable.
      > However, I am stumped as to how to do that in python.
      > Suggestions welcome.[/color]

      Like this?
      [color=blue][color=green][color=darkred]
      >>> class Switchable(obje ct):[/color][/color][/color]
      .... def _plus(self, val):
      .... return val
      .... def _minus(self, val):
      .... return -val
      .... switch = _plus
      .... def sense(self, value):
      .... return self.switch(val ue)
      ....[color=blue][color=green][color=darkred]
      >>> a = Switchable()
      >>> b = Switchable()
      >>> a.sense(2)[/color][/color][/color]
      2[color=blue][color=green][color=darkred]
      >>> b.sense(3)[/color][/color][/color]
      3[color=blue][color=green][color=darkred]
      >>> Switchable.swit ch = Switchable._min us
      >>> a.sense(3)[/color][/color][/color]
      -3[color=blue][color=green][color=darkred]
      >>> b.sense(2)[/color][/color][/color]
      -2

      Seems to work. Might be more elegant to do that
      switch with a method of Switchable. Maybe:

      def flipswitch(self , num):
      if num == 1:
      self.switch = self._plus
      else:
      self.switch = self._minus

      Then you'd switch with, e.g.:

      Switchable.flip switch(1)
      [color=blue]
      > The next best thing, I figure, is to just use a global
      > variable. Several "methods" of the class then check the
      > value of the global variable to determine what to do. The
      > user can then just set the variable to get the desired
      > behavior.[/color]

      Ick.
      [color=blue]
      > However, I tried this and it does not seem to work. I
      > imported the class, then set the global variable. But the
      > new value of the variable somehow did not get back into
      > the class methods that need to see it.[/color]

      Undoubtedly you shadowed it somehow, but without seeing
      the code, I can't guess how.
      [color=blue]
      > Can anyone give me a clue about this? If there is a better
      > way, please let me know. Thanks.[/color]

      Recommend you stick with the first idea, which
      as you see works fine.

      Cheers,
      Terry

      --
      Terry Hancock (hancock@Anansi Spaceworks.com)
      Anansi Spaceworks http://www.AnansiSpaceworks.com

      Comment

      Working...