By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

function without brackets ?

P: n/a
If I call a parameterless function without brackets at the end,
the function is not performed, but ...
I don't get an error message ???

Is this normal behavior ?

thanks,
Stef Mientki
Jan 3 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Stef Mientki <S.**************@mailbox.kun.nltyped
If I call a parameterless function without brackets at the end,
the function is not performed, but ...
If you omit the brackets, you don't actually call the function. Instead
you get a reference to the function object.

Consider this example:

cwd = os.getcwd()
# cwd now contains a string, denoting the current working directory
func = os.getcwd
# func now contains a reference to the function os.getcwd
print func == os.getcwd # prints True
# you can even call it:
cwd_2 = func()
# cwd_2 now also contains a string with the current directory.
print cwd == cwd_2 # prints True, too

--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)
Jan 3 '07 #2

P: n/a
Stef Mientki <S.**************@mailbox.kun.nlwrote in news:57668
$4**********************@news.speedlinq.nl:
If I call a parameterless function without brackets at the end,
the function is not performed, but ...
I don't get an error message ???

Is this normal behavior ?
Yes, it's normal, but you did not in fact call the function, if you
were using Python. Functions are objects that, for example, can be
passed to other functions. The way to refer to the functions
themselves is to omit the arguments and parentheses. So by simply
stating the name of a function, that's all you've done.

--
rzed
Jan 3 '07 #3

P: n/a
On Wed, 2007-01-03 at 15:37 +0100, Stef Mientki wrote:
If I call a parameterless function without brackets at the end,
the function is not performed, but ...
I don't get an error message ???

Is this normal behavior ?
Yes. If you "call" a function without brackets, it's not a call.

Remember that functions are first class objects that can be passed
around like any other object. Hence, Python needs the distinction
between

x = foo() # Assign the *result* of calling foo to x

and

x = foo # Assign the *function* foo itself to x.

You don't get an error message because a function name without
parentheses is a valid expression that refers to that function.

Hope this helps,

Carsten.
Jan 3 '07 #4

P: n/a

Stef Mientki schrieb:
If I call a parameterless function without brackets at the end,
the function is not performed, but ...
I don't get an error message ???

Is this normal behavior ?
Yes, this is perfectly o.k. because each function is a first class
citizen in Python. The difference between foo() and foo is simply that
foo() is the value returned by the function call on foo and foo is a
function object.

Kay

Jan 3 '07 #5

P: n/a
Hope this helps,
thanks You all guys,
It's perfectly clear to me now !

cheers,
Stef
Jan 3 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.