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 ?
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.
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.
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.
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.
>>> 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 ( (see comments)myFunc
, instead, has them in position 0 and 1).
An additional point is, how would you access the parameters otherwise?
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'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With