On Nov 25, 5:48 pm, John O'Hagan <[EMAIL PROTECTED]> wrote: > On Tue, 25 Nov 2008, Marc 'BlackJack' Rintsch wrote: > > On Tue, 25 Nov 2008 07:27:41 +0000, 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 > > > > ? > > > > 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? > > > The question is if `args.a`, `args.b`, …, are semantically part of the > > state of the objects or not. Hard to tell in general. > > Would you mind elaborating a little on that first sentence? > > > > > I know it's a made up example but in the second class I'd ask myself if > > those methods are really methods, because they don't use `self` so they > > could be as well be functions or at least `staticmethod`\s. > > I guess I went overboard keeping the example simple :) : the real case has > many methods, and they all use "self" (except one, actually, so I'm looking > up "static methods" now; thanks). > > Regards, > > John
I'm not sure if you are asking a technical question or a design question. If it helps, I try to think of an object as a thing which has a job to do. If the 'thing' needs information every time to define what it is, or give it a starting state, then that is an argument of __init__() . If I want the object to change or handle something which is a logical task of 'thing', then I give it what it needs via properties or methods (I find I almost never use "public" instance attributes, but then again I am usually writing SDKs which is all about interface). Not sure if that helps... - Rafe -- http://mail.python.org/mailman/listinfo/python-list