By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,258 Members | 1,256 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,258 IT Pros & Developers. It's quick & easy.

"helper function" style question

P: n/a
I have a style question. I have a class with a method, m1, which
needs a helper function, hf. I can put hf inside m1, or I can make it
another method of the class. The only place hf should ever be invoked
is from inside m1.

Is either style preferred?

Here are two short examples:

Helper function as method:

class K(object):
def _hf(self):
print 'Yo'
def m1(self):
self._hf()
return

Helper function as "private" function inside m1.

class K(object):
def m1(self):
def hf():
print 'Yo'
hf()
return

Opinions greatly appreciated.

thanks,
-robert
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I think it is a "personal" style question, but
I like the first example (and use it frequently).
The first method also makes it possible to
use K as a base class object, I'm unsure about
the second method (perhaps one of the class
"gurus" can chime in). I believe that in the
second example, hf() function would only be callable
in m1 method. In first example it is callable from
any method (as self._hf()) and can be called externally
as Kclassinstance._hf() (even though the leading
underline "hides" the method). If a function is
only called from inside of a single method, I question
why you would want a function at all.

HTH,
Larry Bates
Syscon, Inc.

"Robert Ferrell" <fe*****@diablotech.com> wrote in message
news:73**************************@posting.google.c om...
I have a style question. I have a class with a method, m1, which
needs a helper function, hf. I can put hf inside m1, or I can make it
another method of the class. The only place hf should ever be invoked
is from inside m1.

Is either style preferred?

Here are two short examples:

Helper function as method:

class K(object):
def _hf(self):
print 'Yo'
def m1(self):
self._hf()
return

Helper function as "private" function inside m1.

class K(object):
def m1(self):
def hf():
print 'Yo'
hf()
return

Opinions greatly appreciated.

thanks,
-robert

Jul 18 '05 #2

P: n/a
Robert Ferrell wrote:
I have a style question. I have a class with a method, m1, which
needs a helper function, hf. I can put hf inside m1, or I can make it
another method of the class. The only place hf should ever be invoked
is from inside m1.

Is either style preferred?
If either is, it's the first one, not the following one.
class K(object):
def m1(self):
def hf():
print 'Yo'
hf()
return


Here you are wasting time executing the "def" statement
every single time you invoke m1. I'm not sure if that
also means it is compiling it each time (I think not),
but at least it is rebinding the local hf to the function
each time through.

But I would just make hf() a function outside the class.
Since it does not reference anything inside K, it has
nothing inherently to do with K and should be outside,
possibly (eventually) to be moved to another module where
such helper functions reside to make them more maintainable.

-Peter
Jul 18 '05 #3

P: n/a
Your 2nd example makes the most sense from an encapsulation point of
view. If hf is only needed by m1, then it shouldn't be visible outside
of m1. Otherwise it just pollutes the namespace and makes it harder to
move m1 to a separate module/class later on down the road. I use this
technique all the time. I like to create functions, not only when I'm
going to be reusing code, but also simply when it aids the readibility
of my code. And although I'm not sure whether the 2nd technique is
slower than the first (I can imagine that the byte compiler could be
smart enough not to recompile or rebind hf on each invocation of m1), in
general, I don't worry about the performance of my code until I've got
it working correctly. And then it's usually algorithmic changes that
offer the big performance gains (not code reorganization).

Robert Ferrell wrote:
I have a style question. I have a class with a method, m1, which
needs a helper function, hf. I can put hf inside m1, or I can make it
another method of the class. The only place hf should ever be invoked
is from inside m1.

Is either style preferred?

Here are two short examples:

Helper function as method:

class K(object):
def _hf(self):
print 'Yo'
def m1(self):
self._hf()
return

Helper function as "private" function inside m1.

class K(object):
def m1(self):
def hf():
print 'Yo'
hf()
return

Opinions greatly appreciated.

thanks,
-robert


Jul 18 '05 #4

P: n/a

"Paul Morrow" <pm****@yahoo.com> wrote in message
news:cc**********@sea.gmane.org...
slower than the first (I can imagine that the byte compiler could be
smart enough not to recompile or rebind hf on each invocation of m1), in From years ago comments by those in the know, the body of inner function is

compiled just once, as for outer functions, and saved in a code object, but
the function object is recreated each time the def statement is executed.
Rebinding is necessary; so it either recreating the function object or at
least patching a cached version . Consider the following skeleton:

def outer(a, b):
if a:
def inner(x, y=b): pass
else:
def inner(x, y=[b]): pass
return inner

Terry J. Reedy

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.