Function(*number)


#1

What happens when you use the asterisk in the function with the name


#2

The asterisk in a function parameter is often referred to as a splat. It tells Python that the variable may be a package of arguments that will need to be unpacked by the function.

>>> def foo(*args):
    print (len(args))

    
>>> foo(1,2,3,4,5,6,7,8,9)
9
>>> 

#3

So if you put a splat with the argument, your allowed to put multiple arguments in the function but if there is no splat, then you will get an error if you put multiple arguments in the function when a user input occurs?


#4

A single variable can only hold one object, be it a value, a list, or a dictionary. A splat variable can hold multiple objects. Style guides recommend against using them since they are difficult to debug. Explicit parameters are more clear.

Python functions do not permit unnamed parameters, meaning the number of arguments needs to much the number of parameters. The special case would be where there may be zero, one, two, or three, etc. parameters in whch case they would be set with defaults.

>>> def foo(a=1, b=2, c=3):
    return (a, b, c)

>>> foo()
(1, 2, 3)
>>> foo(2,4,6,8)
Traceback (most recent call last):
  File "<pyshell#210>", line 1, in <module>
    foo(2,4,6,8)
TypeError: foo() takes from 0 to 3 positional arguments but 4 were given
>>> def foo():
	return "Foo"

>>> foo('bar')
Traceback (most recent call last):
  File "<pyshell#214>", line 1, in <module>
    foo('bar')
TypeError: foo() takes 0 positional arguments but 1 was given
>>> 

Notice that when we try to pass too many arguments it raises an exception?

In the revised function that takes no arguments, the same error occurs if we try to pass one.

There are always going be some cases where splat is a workable solution to an otherwise indeterminant scenario, but in a well designed program, this should rarely be necessary.

Here is some silliness…

>>> def foo(*args):
    print (len(args))
    args_dict = {}
    args_keys = 'abcdefghijklmnopqrstuvwxyz'
    for i in range(len(args)):
        args_dict[args_keys[i]] = args[i]
    return args_dict

>>> print (foo(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26))
26
{'y': 25, 'k': 11, 'o': 15, 'x': 24, 'h': 8, 'm': 13, 'c': 3, 'u': 21, 't': 20, 'e': 5, 'l': 12, 'a': 1, 'w': 23, 'p': 16, 's': 19, 'r': 18, 'q': 17, 'i': 9, 'f': 6, 'n': 14, 'v': 22, 'j': 10, 'b': 2, 'd': 4, 'g': 7, 'z': 26}
>>> 

#5

Will this lesson be taught later in the Learn Python course because I find this hard to understand.


#6

It’s as expected, for the present. It should be hard to understand for a beginner; I’ve covered a lot of ground, even with the simple example. Forget about the silliness, although it does demonstrate the mechanics of a ridiculous number of packaged arguments. In this case we still had a determined number, but as mentioned earlier, there is no limit as long as Python has memory to work with.

Does this come up in any lessons? Maybe, and if at all, only in one place. It is not something a beginner should be focusing upon. This is an advanced topic.

Just for the fun of it, let’s look at a simpler example, but that is not to say it is a good practice. As mentioned earlier, this is not a best practice.

>>> def triangle_check_angles(*angles):
    A, B, C = angles;
    angles_check = A + B + C == 180
    return "Angles {}, {}, {} check out".format(A,B,C) if angles_check else False

>>> triangle_check_angles(30, 50, 90)
False
>>> triangle_check_angles(30, 60, 90)
'Angles 30, 60, 90 check out'
>>> def triangle_check_sides(*sides):
    a, b, c = sides
    sides_check = a + b > c and a + c > b and b + c > a
    return "Sides {}, {}, {} check out".format(a,b,c) if sides_check else False

>>> triangle_check_sides(3, 4, 8)
False
>>> triangle_check_sides(3, 4, 7)
False
>>> triangle_check_sides(3, 4, 5)
'Sides 3, 4, 5 check out'
>>> 

#7

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