[Tutor] best practices for where to set instance member variables

Gregory, Matthew matt.gregory at oregonstate.edu
Fri Sep 3 21:17:34 CEST 2010


Hi all,

Is there a guideline on where instance member variables should be set within a class?  That is, is it a bad idea to set self variables within private member functions rather than returning them to the enclosing caller?  Or should I avoid calls to internal functions from other member functions altogether (even if they are somewhat complex)?

class Foo:
    def __init__(self, a):
        self._f1(a)
    def _f1(self, a):
        self.a = a

-or-

class Foo:
    def __init__(self, a):
        self.a = self._f1(a)
    def _f1(self, a):
        return a

The first method seems to me to 'hide' where the variables get set, but in looking through some of my site-packages, developers seem to do this both ways.

As always, thanks for help,
matt


More information about the Tutor mailing list