1

I hope this question doesn't seem too naive or silly, but I would just like to get a definite answer as confirmation.

Is it possible to set a variable to call to a function?

i.e

def func():
    print "test"

var = [something magical + func]

then if you had typed/used just var it would output test?

In other words, is there an alternate way to calling a function without the use of parenthesis if it never requires (or accepts) arguments [in Python]?

I am assuming this isn't possible as you always required to use"()" to call a function, but I am curious if there might be any exceptions?

Mark N
  • 326
  • 2
  • 13

5 Answers5

3

In general, no. With the exception of properties as mentioned in comments, you will always need to apply a parameter list (even if it's empty ()) to actually call the function. Otherwise, you're not calling the function, but referencing the function itself as a value.

You can easily reproduce this on Python's command line interpreter:

>>> def func():
...     return "foo"
... 
>>> func() # Actually call the function; statement returns the function's return value
'foo'
>>> func # Reference the function as value; statement returns *the function itself*
<function func at 0x7f2c7a65b938>

>>> var = func # Assign the function value to another variable
>>> var # Now "var" references the same function as "func"...
<function func at 0x7f2c7a65b938>
>>> var() # ...and can also be called as a function
'foo'
>>> 
Community
  • 1
  • 1
helmbert
  • 35,797
  • 13
  • 82
  • 95
  • 1
    This is what I was referring to, thank you. I was just curious as to whether there might have been another possible way to call a function without following the normal use of parenthesis [in Python].. – Mark N Jun 19 '15 at 20:55
2

You're overthinking it:

def func():
    print "test"

var = func
var() # calls func()

In python, functions are objects, so you can assign var to the function func and now it is a reference to func. You can also assign variables to class definitions, because those are objects too:

class A(object):
    def __init__(self):
        self.data = 1

B = A # B is the same class type as A
myA = B() # constructs an A
print(myA.data) # prints "1"
rlbond
  • 65,341
  • 56
  • 178
  • 228
2

For command line usage, there is another trick: __repr__. I use it to call a function just by one single character.

Say you want to call f():

>>> class OneCharFunc(object):
...   def __repr__(self):
...     f()
...     return ''
...
>>> n=OneCharFunc()
>>> n
Berci
  • 544
  • 1
  • 7
  • 10
  • I have made a *decorator* out of this idea: `@Without()` \ `def f(): print "Hey"` \ Call by `f` from command line interpreter. https://github.com/zellerede/utilities/blob/master/util/wp.py – Berci Oct 30 '15 at 14:16
  • Say what!? This is some crazy python magic. Berci is a true wizard :) – Biggsy Feb 28 '23 at 18:14
1

The @property decorator allows you to disguise methods as ordinary attributes. This is useful for things that should be a calculated value, e.g. time remaining in a countdown timer. This ensures a dynamically-updated value for that variable each time it's referenced, because it's really a method call.

>>> class O(object):
...     def __init__(self):
...             self.x = 0
...     @property
...     def x_is_negative(self):
...             return self.x < 0
...
>>> a = O()
>>> a.x_is_negative
False
>>> a.x = -1
>>> a.x_is_negative
True
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0

Try this:

>>> def hello_world(name):
...   print('hello', name)

>>> hello_world('luiz')
hello luiz

>>> hello_world
<function hello_world at 0x0000023C48FC50D0>

>>> a=[hello_world, 'luiz']
>>> a[0](a[1])
hello luiz

>>> type(a[0])
<class 'function'>
>>> type(a[1])
<class 'str'>