Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call function with multiple optional arguments of different types

I have already checked this post and this post, but couldn't find a good way of sorting the problem of my code out.

I have a code as follows:

class foo:
    def __init__(self, foo_list, str1, str2):

        self.foo_list = foo_list
        self.str1 = str1
        self.str2 = str2

    def fun(self, l=None, s1=None, s2=None):

        if l is None:
            l = self.foo_list

        if s1 is None:
            s1 = self.str1

        if s2 is None:
            s2 = self.str2

        result_list = [pow(i, 2) for i in l]

        return result_list, s1[-1], len(s2)

Then I create "f" and call "fun" function:

f = foo([1, 2, 3, 4], "March", "June")
print(f.fun())

The output is:

([1, 4, 9, 16], 'h', 4)

which is correct, but if I do:

print(f.fun("April"))

I get the following error:

TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

Apparently, python confuses the string argument "April" with the list, how do I fix it?

like image 804
Coder Avatar asked Nov 06 '20 23:11

Coder


People also ask

Can a function take multiple arguments?

Functions can accept more than one argument. When calling a function, you're able to pass multiple arguments to the function; each argument gets stored in a separate parameter and used as a discrete variable within the function.

Are function call arguments optional?

So, it is optional during a call. If a value is provided, it will overwrite the default value. Any number of arguments in a function can have a default value. But once we have a default argument, all the arguments to its right must also have default values.

How do you pass multiple optional parameters in TypeScript?

In Typescript, making optional parameters is done by appending the “?” at the end of the parameter name in the function when declaring the parameters and the parameters which are not marked with “?” i.e not optional parameter are called as default parameters or normal parameters where it is must and compulsory to pass ...

How many types of arguments are used to call a function?

5 Types of Arguments in Python Function Definition: default arguments. keyword arguments. positional arguments. arbitrary positional arguments.


1 Answers

By default, the first argument passed to the function will be assigned to the first parameter. If you want to assign the first argument to the second (or n:th) parameter, you must give it as keyword argument. See, for example

In [19]: def myfunc(x='X', y=5):
    ...:     print(x,y)
    ...:
    ...:

# No arguments -> Using default parameters
In [20]: myfunc()
X 5

# Only one positional argument -> Assigned to the first parameter, which is x
In [21]: myfunc(100)
100 5

# One keyword argument -> Assigned by name to parameter y
In [22]: myfunc(y=100)
X 100

The type of the arguments do not matter, but the order you used in the function definition.

Notes on terminology

  • By parameter, I mean the variable in the function definition
  • By argument, I mean the actual value passed to the function.
like image 61
np8 Avatar answered Sep 21 '22 03:09

np8