On 2008-11-25 08:27, John O'Hagan wrote: > > Is it better to do this: > > class Class_a(): > def __init__(self, args): > self.a = args.a > self.b = args.b > self.c = args.c > self.d = args.d > def method_ab(self): > return self.a + self.b > def method_cd(self): > return self.c + self.d > > or this: > > class Class_b(): > def method_ab(self, args): > a = args.a > b = args.b > return a + b > def method_cd(self, args) > c = args.c > d = args.d > return c + d > > ?
That depends entirely on what you intend to do with objects of Class_a and Class_b. In the first case, you are persisting the argument attributes in the object, in the second case, you are merely working on them - just like you would in a function. > Assuming we don't need access to the args from outside the class, > is there anything to be gained (or lost) by not initialising attributes that > won't be used unless particular methods are called? It is always good practice to provide default values for instance variables in the class definition, both to enhance readability and to allow adding documentation regarding the variables, e.g. class Class_a: # Foo bar a = None # Foo baz b = None ... -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Nov 25 2008) >>> Python/Zope Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ 2008-11-12: Released mxODBC.Connect 0.9.3 http://python.egenix.com/ :::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 -- http://mail.python.org/mailman/listinfo/python-list