[Tutor] self.name vs. passing a name
Kent Johnson
kent37 at tds.net
Tue Jul 28 03:39:30 CEST 2009
On Mon, Jul 27, 2009 at 7:39 PM, Che M<pine508 at hotmail.com> wrote:
> 1.Pass the variable to the second function.
>
> def calculate_something(self):
> answer = self.do_first_calculation() #1st function returns answer
> self.do_second_calculation(answer) #2nd is passed answer and uses it.
>
> 2. Create the variable in the class instance scope and use that in the
> second function.
>
> def calculate_something(self):
> self.do_first_calculation() #1st function creates
> self.answer
> self.do_second_calculation() #2nd uses self.answer
>
> Both of these approaches can work, but I would like to better understand
> when it is best to do one or the other.
i would use the first method.
- it makes explicit that do_first_calculation() is computing something
needed by the second
- you could give the variable a better name which would make it easier
to understand calculate_something()
- answer is not part of the state of self and it has no meaning
outside of calculate_something(), so don't clutter up self with the
extra value.
kent
More information about the Tutor
mailing list