Simple question about variables

John Hunter jdhunter at ace.bsd.uchicago.edu
Thu Dec 5 10:46:03 EST 2002


>>>>> "laotseu" == laotseu  <bdesth at perso.free.fr> writes:


    laotseu> <IMHO> Yep, but you come with the same problem that make
    laotseu> globals bad : 

        true, but the scope of the problem is much narrower <wink>

    laotseu> you can't say if myclass.proc1() has been
    laotseu> called before myclass.proc2() is. I mean that instances
    laotseu> attributes are somehow global to a set of functions for a
    laotseu> particular piece of data, and methods relies on (or must
    laotseu> check for...) a particular state. 

Fair enough.  But I use this kind of design fairly often and it works
well for me

class sumthin:

  def func1(self, someargs):
    self._func1Var = do_something_with(someargs)

  def func2(self, otherargs):
    try: var = self._func1Var
    except AttributeError: var = someDefault
    do_something__else_with(var, otherargs)

Of course, this solution requires that some reasonable default
behavior exists.

John Hunter




More information about the Python-list mailing list