[Tutor] self.name vs. passing a name

Alan Gauld alan.gauld at btinternet.com
Tue Jul 28 10:54:04 CEST 2009


"Che M" <pine508 at hotmail.com> wrote

> Let's say you have a sequence of two calculations ...each one
> done as a function called by an overall calculate_something() function.

> The "answer" to each function is then used in the next function.
> I can think of two ways to make that answer available ...
>   1) pass it in, or
>   2) declare it as self.answer

The first is the best solution in the general case however,
1)  if your functions are methods of a class AND
2) you can use the intermediate result for something useful
    elsewhere in the class

then you could use the second method.
Otherwise the second merthod is just a variation in storing the
result in a global variable wjhich is generally considered a bad
design style.

In general keep data as close to its point of use as possible.


HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list