Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Instance attributes vs method arguments

Reply
Thread Tools

Instance attributes vs method arguments

 
 
John O'Hagan
Guest
Posts: n/a
 
      11-25-2008


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?

Thanks,

John O'Hagan
 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      11-25-2008
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.

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.

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
 
 
 
bieffe62@gmail.com
Guest
Posts: n/a
 
      11-25-2008
On 25 Nov, 08:27, John O'Hagan <(E-Mail Removed)> 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?
>
> Thanks,
>
> John O'Hagan


If 'args' is an object of some class which has the attribute a,b,c,d,
why don't you just add
method_ab and method_cd to the same class, either directly or by
sibclassing it?

If for some reason you can't do the above, just make two functions:

def function_ab(args): return args.a + args.b
def function_cd(args): return args.c + args.d

One good thing of Python is that you don't have to make classes if you
don't need to ...

Ciao
------
FB
 
Reply With Quote
 
John O'Hagan
Guest
Posts: n/a
 
      11-25-2008
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


 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      11-25-2008
On Tue, 25 Nov 2008 10:48:01 +0000, John O'Hagan 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
>> > […]

>>
>> 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?


Do `self.a`, `self.b`, …, define the state of a `Class_a` instance or
not? One can't answer that question without knowing the meaning of the
class and the attributes.

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
Rafe
Guest
Posts: n/a
 
      11-25-2008
On Nov 25, 5:48*pm, John O'Hagan <(E-Mail Removed)> 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
 
Reply With Quote
 
John O'Hagan
Guest
Posts: n/a
 
      11-26-2008
On Tue, 25 Nov 2008, Rafe wrote:
> On Nov 25, 5:48*pm, John O'Hagan <(E-Mail Removed)> 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...
>

You've picked up my fundamental confusion! Thanks to your reply and others I
think that's cleared up for me now, which just leaves the technical
question: insofar as one is only interested in accessing methods, is there an
difference in efficiency (for large enough number of methods and arguments)
between

a) passing all arguments to __init__() and accessing them via self within
individual methods:

class = Class(all_class_args)
class.method_a()
class.method_b()
...
or

b) passing the arguments needed by each method when it is called on an
instance:

class = Class()
class.method_a(a_args)
class.method_b(b_args)
...

Thanks to all,

John


 
Reply With Quote
 
George Sakkis
Guest
Posts: n/a
 
      11-26-2008
On Nov 25, 8:49*pm, John O'Hagan <(E-Mail Removed)> wrote:

> is there an
> difference in efficiency (for large enough number of methods and arguments)
> between
>
> a) passing all arguments to __init__() and accessing them via self within
> individual methods:
>
> class = Class(all_class_args)
> class.method_a()
> class.method_b()
> ...
> or
>
> b) passing the arguments needed by each method when it is called on an
> instance:
>
> class = Class()
> class.method_a(a_args)
> class.method_b(b_args)
> ...


The difference in API here is more crucial than the difference in
performance. Deciding between the two based on the (guessed or
measured) performance improvement misses the point of OO design.

George
 
Reply With Quote
 
John O'Hagan
Guest
Posts: n/a
 
      11-26-2008
On Wed, 26 Nov 2008, Ben Finney wrote:
> John O'Hagan <(E-Mail Removed)> writes:
> > insofar as one is only interested in accessing methods, is there an
> > difference in efficiency (for large enough number of methods and
> > arguments) between
> >
> > a) passing all arguments to __init__() and accessing them via self
> > within individual methods:
> >
> > class = Class(all_class_args)
> > class.method_a()
> > class.method_b()
> > ...
> > or
> >
> > b) passing the arguments needed by each method when it is called on
> > an instance:
> >
> > class = Class()
> > class.method_a(a_args)
> > class.method_b(b_args)
> > ...

>
> Note that you've chosen confusing names for the above. When you call
> the class, the return value will be an *instance of* the class, so
> binding the name ‘class’ to that return value has two problems: it's a
> misnomer, and it's a syntax error because ‘class’ is a reserved word.


I know, sorry, just a simplified example...s/class/class_instance/.

John


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
data attributes override method attributes? Jayden Python 16 09-29-2012 03:20 AM
Re: Instance attributes vs method arguments M.-A. Lemburg Python 4 11-26-2008 07:23 PM
why can't an instance instantiated within a class method access aprotected instance method? Greg Hauptmann Ruby 9 06-16-2008 10:16 AM
instance method adding another instance method to the class Raj Singh Ruby 2 05-29-2008 10:09 PM
Can you set a class instance's attributes to zero by setting the instance to zero? Gerard Flanagan Python 3 11-19-2005 06:58 PM



Advertisments