Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to declare a function without arguments but then pass some arguments to that function without raising exception?

In python is it possible to have the above code without raising an exception ?

def myfunc():
    pass

# TypeError myfunc() takes no arguments (1 given)
myfunc('param')

Usually in php in some circumstances I launch a function without parameters and then retrieve the parameters inside the function.

In practice I don't want to declare arguments in myfunc and then passing some arguments to it. The only one solution I found is myfunc(*arg). Are there any other methods ?

like image 470
yuri Avatar asked Feb 10 '10 23:02

yuri


People also ask

Can a function work without arguments?

Functions do not have declared return types. A function without an explicit return statement returns None . In the case of no arguments and no return value, the definition is very simple. Calling the function is performed by using the call operator () after the name of the function.

Can we call a function without passing arguments?

So, if we call the function without passing the arguments for them, it will throw an error. For the optional parameters, we don't have to pass any arguments. If we don't, the default value will be used.

Do all functions need an argument?

You can define a function that doesn't take any arguments, but the parentheses are still required. Both a function definition and a function call must always include parentheses, even if they're empty.

Can you pass a function as an argument?

Functions are data, and therefore can be passed around just like other values. This means a function can be passed to another function as an argument. This allows the function being called to use the function argument to carry out its action.


2 Answers

>>> def myFunc(*args, **kwargs):
...   # This function accepts arbitary arguments:
...   # Keywords arguments are available in the kwargs dict;
...   # Regular arguments are in the args tuple.
...   # (This behaviour is dictated by the stars, not by
...   #  the name of the formal parameters.)
...   print args, kwargs
...
>>> myFunc()
() {}
>>> myFunc(2)
(2,) {}
>>> myFunc(2,5)
(2, 5) {}
>>> myFunc(b = 3)
() {'b': 3}
>>> import dis
>>> dis.dis(myFunc)
  1           0 LOAD_FAST                0 (args)
              3 PRINT_ITEM
              4 LOAD_FAST                1 (kwargs)
              7 PRINT_ITEM
              8 PRINT_NEWLINE
              9 LOAD_CONST               0 (None)
             12 RETURN_VALUE

And to actually answer the question: no, I do not believe there are other ways.

The main reason is pretty simple: C python is stack based. A function that doesn't require parameters will not have space allocated for it on the stack (myFunc, instead, has them in position 0 and 1). (see comments)

An additional point is, how would you access the parameters otherwise?

like image 93
badp Avatar answered Sep 20 '22 18:09

badp


There are two ways to pass args in

By Position

>>> def myfunc(*args):
...  print "args", args
...
>>> myfunc("param")
args ('param',)

By Keyword

>>> def myfunc(**kw):
...  print "kw", kw
... 
>>> myfunc(param="param")
kw {'param': 'param'}

And you can use a combination of both

>>> def myfunc(*args, **kw):
...  print "args", args
...  print "kw", kw
... 
>>> myfunc("param")
args ('param',)
kw {}
>>>
>>> myfunc(param="param")
args ()
kw {'param': 'param'}
>>>
>>> myfunc("param", anotherparam="anotherparam")
args ('param',)
kw {'anotherparam': 'anotherparam'}
like image 27
John La Rooy Avatar answered Sep 17 '22 18:09

John La Rooy