Conditional function arguments?



In Erlang, you are able to pass what I think are called conditional function arguments (please let me know if my terminology is incorrect) which allows me to write a function such as:

foobar("string", A) ->

This function will accept 2 arguments, but the 1st one has to be "string", otherwise it wont run.
I was wondering if you can do something similar in Python, and to what extent?
If you can't, how do you go about creating conditions for your functions so that it wont run unless expected arguments are passed to it? Is it only possible to do it with the "if" condition?


there is *args and **kwargs, which means you can pass in many arguments:

def example(*args):
    print args

example("hello", "to", "the", "world")

And you can pass a default value if an argument is not supplied:

def example(a=5):
    print a

but that is about it, the rest will have to be done using conditions (if) within the function itself


there is a built in type() function which gives you the data type, but still, this would have to be used with an if condition, its not like erlang


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.