Coding Style: Defining Functions within Methods?
Eddie Corns
eddie at holyrood.ed.ac.uk
Fri Sep 5 14:38:47 EDT 2003
harry.pehkonen at hotpop.com (Harry Pehkonen) writes:
>I have been defining new class methods when I'm trying to simplify
>some code. But I'm thinking I should just define functions within
>that method because they aren't useful from the outside anyway.
>Example:
>Before:
>class Mess(object):
> def complicated(self, count):
> for i in count:
> self.do_loop(i)
> def do_loop(self, i):
> ...whatever...
>After:
>class Cleaner(object):
> def complicated(self, count):
> def do_loop(i)
> ...whatever...
> for i in count:
> do_loop(i)
>The point is that do_loop is now not ``contaminating'' things. I
>suppose do_loop could be __do_loop, but it would still show up in
>places where I don't think it should (such as dir(Mess)).
>Thoughts?
Definitely useful, especially as the inner function can see the same variables
as the outer (though I do keep getting bitten when I expect to be able update
variables).
Eddie
More information about the Python-list
mailing list