Forgetting "()" when calling methods

Terry Reedy tjreedy at udel.edu
Sat Apr 26 00:21:48 EDT 2003


"Frantisek Fuka" <fuka at fuxoft.cz> wrote in message
news:b8cadp$1fj9$1 at ns.felk.cvut.cz...
> When I try to call methods, I sometimes forget to include the
> parentheses. Instead of:

In Python, '()' is, in the proper context, the 'call me with the
contained (possible none) arguments' operator.  So the above is much
like saying, "When I try to calculate products, I sometimes forget to
include the asterisk' -- like mathemeticians are in the habit of
doing -- and then asking that interpreter start guession that missing
binary operators are implied multiplication.

Python code in not math notation.

> if object.isGreen():
> do something...
>
> i sometimes write:
>
> if object.isGreen:
> do something...
>
> If I understand it correctly,

which you do not, quite yet ;-)

> the if statement in this case tests if  pointer to hasParent method
is non-zero,

In Python, every expression evaluates to a Python object.  In
expressions, bare names evaluate to the object they are bound to in
the the appropriate namespace.  By default, absent user written
behind-the-scenes trickery, dotted (attribute) names evaluate to the
object they are bound to in the preceeding objects private namespace.
In any case, if statements branch according to the boolean value of
the object resulting from the conditional expression.  The boolean
value of function objects is always True.

Pointers (and memory addresses) are not part of the definition of
Python.  Python is not C.

> You can say to me "Don't forget to always include the parenteses"

Just as you should not forget the asterisk in multiplications.

> I'm still curious if this cannot be somehow configured,

The consistency of Python expression evaluation is one of its basic
features.  Fighting it is the path to frustration.

> the possibility of accessing the method pointers but I usually don't
> need it and for a beginner like me this makes the applications very
hard
> to debug.

Have you never yet typed in 'obj.meth.__doc__' at the interactive
interpreter prompt?  That is possible precisely because 'obj.meth' is
precisely what it claims to be.  Also, it should not be too long
before you want to start abbreviating obj.meth and do something like

stack = []
push = stack.append
pop  = stack.pop
#code using these three names, simple example being
push(1); print stack
# [1]
print pop(), stack
# 1, []

Terry J. Reedy






More information about the Python-list mailing list