Instance attributes vs method arguments

Ben Finney bignose+hates-spam at benfinney.id.au
Tue Nov 25 22:05:08 EST 2008


John O'Hagan <mail at johnohagan.com> 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.

As for your actual question: Write code that's clear in intent, and
worry about efficiency only when you *measure* a performance problem.

-- 
 \     “First they came for the verbs, and I said nothing, for verbing |
  `\    weirds language. Then, they arrival for the nouns and I speech |
_o__)                           nothing, for I no verbs.” —Peter Ellis |
Ben Finney



More information about the Python-list mailing list