Variable number of arguments Part 1

Variable number of arguments Part 1

Lesson Details:
June 29, 2020


I: Introduction

This article will describe about the basic python programming for beginner. I’m usually using Python for backend web development, but I’ve also used it for teaching people how to program. It is a language that’s easy to learn and use, but you can still do complex things with it. The core Python language has been frozen since Python 2.0 was released in 2000, and there won’t be new major releases. However, the standard library and the set of third-party libraries (or “modules”) that add functionality to the language keep growing. This article will help anyone who wants to learn the basics on python programming.

II: Body

A: Variable number of arguments part 1

Python has some interesting features, which make it quite different from most other languages. One of these is that you can pass a variable number of arguments to a function or method call. This is something that can be very useful, but it is also one of the most difficult things to get used to in Python.

So, let’s start simply. First of all, let’s define a simple function:

>>> def say_hello(): return 'Hello World' >>> say_hello() 'Hello World' >>>

The name of this function is say_hello , and it doesn’t take any arguments. Now let’s try calling it with a variable number of arguments:

>>> say_hello('Who', 'are', 'you?') 'Hello Who are you?' >>>

It works! You can see that it took three arguments, and returned a string containing them in order. You may have noticed that in the output, the order in which the arguments were passed in matched the order in which they appeared in the string. Unlike some other languages, Python does not enforce any ordering when passing arguments to a function. Any argument can go in any position! This can make things a little confusing when invoking a function with a variable number of arguments, but once you get used to it, it can be very useful. In fact, you can even pass an infinite number of arguments! Here’s how:

>>> say_hello('Who', 'are', 'you?', 'Here', 'I') 'Hello Who are you? Here I' >>>

It might seem a little strange at first, but it is definitely possible. Let’s expand on this a bit by creating a function that takes an unlimited number of arguments:

def say_hello(args): for arg in args: print arg return "Hello " + str(args[0]) + "." # Calling this function with a fixed number of arguments gives the same result as before: say_hello('Who', 'are', 'you?', 'Here') # But.. # Calling this function with an unlimited number of arguments will cause all but the first argument # to be ignored: say_hello('Who', 'are', 'you?', 'Here', 'You', 'too', 'Me') # Or just by providing just one argument, we can call it with no arguments at all: say_hello('No arguments.') # We can even call it with an empty list as an argument: say_hello([]) # And you can even give it more than one argument! say_hello(['a', 'b'], ['c'], ['d']) # There is no limit! (Except memory, perhaps?) # We can even call it without arguments at all by providing an empty list as the only argument! say_hello(['No arguments.']) # But if we try calling it without any arguments at all.. say_hello() # We'll get an error because the empty list ([]), which Python automatically returns if there are no arguments, is not what we're looking for! TypeError: say_hello() takes exactly 1 argument (0 given) >>>

As you can see, there is no limit to how many arguments you can pass to a function or method! But now I must warn you… If you are writing code that will be called by another piece of code being written by someone else, be careful not to forget how many arguments are being passed in. If you don’t have any way of knowing how many arguments are being passed in, then be careful to check whether or not the function was called with an empty list before doing anything with it. Otherwise, your code could break if someone accidentally or purposefully passes in an empty list. Although this method is quite convenient, there are some disadvantages that have nothing to do with security issues. For example, if you want to use the same function with slightly different arguments at different times, you either have to create multiple functions or copy-paste large chunks of code each time you want to change something! This is where named arguments come in handy. Named arguments are basically just regular arguments, except they have names assigned to them instead of just positional values. The syntax for calling a function with named arguments looks like this: hello(-who='No one', -how='very much') . Now let’t actually write this function:

def hello(who='No one', how='very much'): print who print how hello() hello('Sally') hello('John', how='muchly') hello() hello(_who='Bob') hello(_how='so', _who='Tom') hello() hello(_how='very much', _who='no one') hello('Jill') hello("May", _how='Wee!') hello(_who='Sally again') hello(_how='calmly') # Be careful not to forget your trailing commas!! As mentioned earlier, Python uses whitespace as the delimiter between lists and tuples and between items on lists and tuples. So if we forget our commas here.. hello() hello() hello() hello() Hello No one very much very muchly No one No one No one Bob so muchly very much No one No one No one No one No one No one Jill May Wee! calmly No one Sally again No one No one No one No one No one No one No one No one No one No one No one No one No one No one No one No one No one No one No one >>>

Notice how we called each argument by its name ( _how and _who ) instead of its position ( 0 and 1 ). Also notice that we had to put those trailing commas! If we didn’t put those trailing commas before our final two calls to this function, then Python would have given us an error because those final two calls each have only a single comma separating items on the list. Even though I said that there are some disadvantages to using variable numbers of arguments and named arguments, my personal opinion is that they are so convenient, especially when passing information from a GUI into a program or vice versa. This is especially true when dealing with very long filenames or URLs! So my opinion is that they should definitely be used whenever possible!

loader
Course content