473,396 Members | 2,081 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,396 software developers and data experts.

Calling Function Without Parentheses!

What a debug nightmare! I just spent HOURS running my script through
the debugger, sprinkling in log statements, and the like, tracking down
my problem.

I called a function without the ending parentheses. I sure do WISH
Python would trap it when I try to do the following:
MyFunc

instead of:

MyFunc()

aaaaaaaaaaaah.

Jul 18 '05 #1
11 9046

Kamilche> I called a function without the ending parentheses. I sure do
Kamilche> WISH Python would trap it when I try to do the following:
Kamilche> MyFunc

Kamilche> instead of:

Kamilche> MyFunc()

Google for pychecker.

Skip
Jul 18 '05 #2
Kamilche wrote:
What a debug nightmare! I just spent HOURS running my script through
the debugger, sprinkling in log statements, and the like, tracking down my problem.

I called a function without the ending parentheses. I sure do WISH
Python would trap it when I try to do the following:
MyFunc

instead of:

MyFunc()


You're a former Pascal programmer, aren't you? ;-)

In Python, it's not an error, because you can do things like:
def simpson(f, a, b): .... "Simpson's Rule approximation of the integral of f on [a, b]."
.... return (b - a) * (f(a) + 4 * f((a + b) / 2.0) + f(b)) / 6.0
.... simpson(math.sin, 0.0, math.pi) # Note that math.sin is a function

2.0943951023931953

Jul 18 '05 #3
Kamilche wrote:
What a debug nightmare! I just spent HOURS running my script through
the debugger, sprinkling in log statements, and the like, tracking down my problem.

I called a function without the ending parentheses. I sure do WISH
Python would trap it when I try to do the following:
MyFunc

instead of:

MyFunc()

aaaaaaaaaaaah.


Aaaaaaaaaaaah indeed. You must be using an extremely old version of
pychecker. The version I have in my Python22 directory gave the same
results as the current one; see below.

C:\junk>type noparens.py
[bangs inserted to defeat Google's lstrip()
!def bar():
! foo
!def foo():
! alist = []
! alist.sort

C:\junk>pychecker noparens.py

C:\junk>c:\python24\python.exe
c:\python22\Lib\site-packages\pychecker\checker.py noparens.py
Processing noparens...

Warnings...

noparens.py:2: Statement appears to have no effect
noparens.py:5: Statement appears to have no effect

Jul 18 '05 #4

Dan Bishop wrote:
Kamilche wrote:
What a debug nightmare! I just spent HOURS running my script through
the debugger, sprinkling in log statements, and the like, tracking down
my problem.

I called a function without the ending parentheses. I sure do WISH
Python would trap it when I try to do the following:
MyFunc

instead of:

MyFunc()


You're a former Pascal programmer, aren't you? ;-)

In Python, it's not an error, because you can do things like:
def simpson(f, a, b): ... "Simpson's Rule approximation of the integral of f on [a, b]."
... return (b - a) * (f(a) + 4 * f((a + b) / 2.0) + f(b)) / 6.0
... simpson(math.sin, 0.0, math.pi) # Note that math.sin is a

function 2.0943951023931953


In Python, it's not an error, because functions are first class
citizens. The OP's problem is evaluating an expression and then doing
SFA with the result. Pychecker appears to be able to make the
distinction; see below:

C:\junk>type simpson.py
import math
def simpson(f, a, b):
return (b - a) * (f(a) + 4 * f((a + b) / 2.0) + f(b)) / 6.0
print simpson(math.sin, 0.0, math.pi)

C:\junk>python simpson.py
2.09439510239

C:\junk>pychecker simpson.py

C:\junk>c:\python24\python.exe
c:\python22\Lib\site-packages\pychecker\checker.py simpson.py
Processing simpson...
2.09439510239

Warnings...

None

Jul 18 '05 #5
Yep. Definitely time download PyChecker. Thanks for the tip!

Jul 18 '05 #6

"Kamilche" <kl*******@comcast.net> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
What a debug nightmare! I just spent HOURS running my script through
the debugger, sprinkling in log statements, and the like, tracking down
my problem.
Did you try PyChecker? (Don't know if
I called a function without the ending parentheses.
Which means you didn't call, and that was your problem ;-) In Python, the
(...) pair in the appropriate context (where an operator is expected) *is*
the infix/postfix call operator. It is equivalent to the call or gosub
prefix in some other languages. The call operator works with any callable,
not just function objects.
I sure do WISH Python would trap it when I try to do the following:
trap = raise an exception? nooooh.
MyFunc
instead of:
MyFunc()


In Python, non-keyword names resolve at runtime to the objects they are
then bound to. This simple, uniform rule is, to me, part of the beauty of
Python. There are lots of times that one wants to refer to a callable
without calling it. Indeed, because Python separates referring to an
object from calling the object, it can and does have a broader notion of
'callable' than other languages. This includes the option of making
instances of any user class callable (by including a __call__ method).

Terry J. Reedy


Jul 18 '05 #7
Kamilche wrote:
What a debug nightmare! I just spent HOURS running my script through
the debugger, sprinkling in log statements, and the like, tracking down
my problem.

I called a function without the ending parentheses. I sure do WISH
Python would trap it when I try to do the following:
MyFunc

Actually you want use a method as an ordinary variable without calling
it in many cases. It is often used in a dynamic language.

A simple example is:

result = []
a = result.append
if something:
a('some result')
elif something_else:
a('another result')
else:
a('default result')

--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
Jul 18 '05 #8
Yeah, but still. If they even had the most basic check, like 'an object
is being referred to on this line, but you're not doing anything with
it' would be handy in catching that. When you use an object like that,
usually you're doing something with it, like assigning it to a variable.

Jul 18 '05 #9
Kamilche wrote:
Yeah, but still. If they even had the most basic check, like 'an object
is being referred to on this line, but you're not doing anything with
it' would be handy in catching that. When you use an object like that,
usually you're doing something with it, like assigning it to a variable.


In many cases, however, it's not possible to distinguish this.

def get_pi():
import math
return math.pi

print my_func(get_pi)

Now, am I trying to pass the function object get_pi into my_func(), or
do I want to call get_pi() and pass the return value?

There are also many times when it's sensible to do nothing with an
object reference -- i.e. ignoring the return value of a function which
you're calling for its side-effects.

It seems to me that it's reasonable for the Python interpreter to
*not* attempt to guess at whether a questionable usage is an error or
not. Better to have that done by a developer tool (pychecker) than
through runtime checks every time the program is used.

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #10
Uh, you're right! I wouldn't want to bog Python down with even more
checking at run time. I guess I'm asking for syntax checks that are
typically done only with compiled languages.

It doesn't stop me from using Python, though! Since I don't have syntax
checking, I find I have to test supporting routines thoroughly, to make
sure it errs when it should err and runs when it should run. That's
something that's always good to do, but it's especially useful in
Python, which has no syntax checking before runtime.

Jul 18 '05 #11
Kamilche wrote:
Uh, you're right! I wouldn't want to bog Python down with even more
checking at run time. I guess I'm asking for syntax checks that are
typically done only with compiled languages.


In that case, I can suggest having a look at Pychecker, which performs static
sanity checks on Python code.
http://pychecker.sourceforge.net/

Cheers,
Nick.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Ken VdB | last post by:
Hi everyone, Is there a reason why the Mid() function only works in one direction in VBScript? This code works in VB6 but not in VBScript? Is there a way around it? I am trying to create an...
4
by: Steve | last post by:
Hi I have decided it is time to learn Javascript (I know PHP fairly well, so I thought it shouldn't be too difficult!!!) I have a couple of question though, which I can highlight with the...
2
by: neptune | last post by:
I have limited knowledge of VBA, but I've learned a lot here. I can't figure out how to pass 2 parameters in a sub? I call PassDate and pass 1 date. Public Sub PassDate() Calculate...
5
by: Zlatko Matić | last post by:
Hello. How can I call some functions on MSDE when working in .mdb ? Especially in-line functions which are similar to stored procedures. How can I use MSDE in-line functions as recordsource for...
17
by: Billy Patton | last post by:
In c can I declare a default value for an function argument? int X(int a,int b=10); int main() { X(1); } int X(int a,int b)
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
7
by: zheetee | last post by:
<script type="text/vbscript"> sub setTextBoxValue(a1,a2) thedelete.deleteTextBox.value = a1 end sub </script> <td> <a href="#"...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
8
by: Yansky | last post by:
If I have the following function: function foo(){ alert('hi'); } and I don't need to pass any parameters to it, is calling it this way:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.