"helper function" style question

Discussion in 'Python' started by Robert Ferrell, Jul 12, 2004.

  1. 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
    Robert Ferrell, Jul 12, 2004
    #1
    1. Advertising

  2. Robert Ferrell

    Larry Bates Guest

    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" <> wrote in message
    news:...
    > 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
    Larry Bates, Jul 12, 2004
    #2
    1. Advertising

  3. Robert Ferrell

    Peter Hansen Guest

    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
    Peter Hansen, Jul 12, 2004
    #3
  4. Robert Ferrell

    Paul Morrow Guest

    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
    Paul Morrow, Jul 13, 2004
    #4
  5. Robert Ferrell

    Terry Reedy Guest

    "Paul Morrow" <> wrote in message
    news:ccv6ck$blt$...
    > 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=): pass
    return inner

    Terry J. Reedy
    Terry Reedy, Jul 13, 2004
    #5
  6. Robert Ferrell

    Paul Morrow Guest

    Terry Reedy wrote:
    > "Paul Morrow" <> wrote in message
    > news:ccv6ck$blt$...
    >
    >>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=): pass
    > return inner
    >
    > Terry J. Reedy
    >
    >
    >


    But if the inner defs were not conditionally compiled (not inside of the
    if stmnt --- as in the example we were considering), the compiler
    *could* detect that and not have to rebind the local variable (inner)
    each time. In fact, it could even inline the helper function
    (eliminating the function call entirely), thereby speeding things up
    even more.

    But even if the Python compiler doesn't do that, I still prefer defining
    things so that they are visible only within the scope where they are
    needed. That generally makes program maintenance/comprehension easier,
    which translates into saving 'my' time...

    Paul
    Paul Morrow, Jul 13, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chumley the Walrus

    Subroutine /helper function error

    Chumley the Walrus, Jun 10, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    321
    bruce barker
    Jun 10, 2004
  2. Chumley the Walrus
    Replies:
    2
    Views:
    467
    John Saunders
    Jun 15, 2004
  3. D. Shane Fowlkes

    Hyperlink Column & Helper Function not working

    D. Shane Fowlkes, Nov 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    820
    =?Utf-8?B?RU5JWklO?=
    Nov 19, 2004
  4. D. Shane Fowlkes
    Replies:
    1
    Views:
    410
    Marina
    May 19, 2005
  5. Ken Varn
    Replies:
    0
    Views:
    443
    Ken Varn
    Apr 26, 2004
Loading...

Share This Page