473,881 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9113

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.si n, 0.0, math.pi) # Note that math.sin is a function

2.0943951023931 953

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>pycheck er noparens.py

C:\junk>c:\pyth on24\python.exe
c:\python22\Lib \site-packages\pychec ker\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.si n, 0.0, math.pi) # Note that math.sin is a

function 2.0943951023931 953


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.si n, 0.0, math.pi)

C:\junk>python simpson.py
2.09439510239

C:\junk>pycheck er simpson.py

C:\junk>c:\pyth on24\python.exe
c:\python22\Lib \site-packages\pychec ker\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*******@comc ast.net> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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

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

Similar topics

10
5967
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 ASP page which produces a fixed width text file for import into a third party legacy application which I don't have control over. --code sample-- Dim strSomeString
4
5418
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 following code... --- function mysortfn(a,b) {
2
1723
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 (#7/1/2003#) End Sub Public Sub Calculate(var1 As Date) 'Runs Code
5
2647
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 .mdb forms ? Can I call in-line functions using ADO ? I tried, but it seems that only stored procedures are allowed (adCmdStoredProc).... Thanks.
17
1911
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
1882
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 shows the alert. What is the essential reason? function () { alert('hi mom'); }(); function () { alert('hi dad'); }(8); var x=function () { alert('hi bro'); }();
7
5243
by: zheetee | last post by:
<script type="text/vbscript"> sub setTextBoxValue(a1,a2) thedelete.deleteTextBox.value = a1 end sub </script> <td> <a href="#" onclick="setTextBoxValue('<%#Container.DataItem("SerialNumber")%>','<%#Container.DataItem("EquipmentCategory")%>')">
26
4896
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 about the syntax of calling the same. #include <stdio.h> void fp1()
8
2951
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
11098
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10401
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7953
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7109
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.