473,378 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

"helper function" style question

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
4 8868
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ¤ Alias | last post by:
I have a function named getID3info (lvwDiscInfo.SelectedItem). What is the difference between getID3info (lvwDiscInfo.SelectedItem) and Call getID3info(lvwDiscInfo.SelectedItem) ?
5
by: Michael Stevens | last post by:
Probably the wrong wording but since I'm not a scripter I won't claim to know what I'm talking about. I got this script from www.htmlgoodies.com <script language="JavaScript"> <!--...
10
by: Lionel B | last post by:
Greetings, I cannot figure out why the following code does not compile: --- BEGIN CODE --- typedef double ftype(double); struct A {
1
by: Perttu Pulkkinen | last post by:
I would like to have a javasript/jscript function template/"framwork" instead of checking browsers by name. The shortness of script in my opinion is not the goal, but clearness and ease iof...
2
by: craigkenisston | last post by:
Hi, I'm looking to achieve the same functionality as the "like" function in SQL (Server) in a program in C#. Is there anything out of the box ready to use ? Can this be achieved with regex...
1
by: Sam Johnson | last post by:
Hi I've seen that the string classes in the .net framework have a cool (new?) function: splitting strings and returning the substrings in an array. Is there any equivalent to this in "old-style"...
3
by: Chen ShuSheng | last post by:
HI, I am now study a segment of codes: ------------------------ printf("%p\t",fp); /*add by me*/ fseek(fp, 0L, SEEK_END); /* go to end of file */ printf("%p\t",fp); ...
4
by: BobRoyAce | last post by:
Let's say I have a class called ClassA which has a bunch of member variables, properties, private and public methods. Then, let's say that I want to create another class, ClassACalcEngine, that...
6
by: grbgooglefan | last post by:
I am creating functions, the return result of which I am using to make decisions in combined expressions. In some expressions, I would like to inverse the return result of function. E.g....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.