Download presentation
Presentation is loading. Please wait.
Published byDorothy Bond Modified over 8 years ago
1
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
2
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops – still! Conditionals – still! File I/O, catching Exceptions. Exercises 1 to 5. Sets not on quiz. Winter 2016CISC101 - Prof. McLeod2
3
Today… Functions, Cont.: –Go over Keyword and Default arguments again. –Variable numbers of positional arguments. –Using None as a default argument. –Checking argument types. Next topic is Strings. Winter 2016CISC101 - Prof. McLeod3
4
4 Keyword Arguments Suppose you have a function with several parameters, but you don’t want to worry about supplying values in the matching order. You can use keyword arguments to supply the arguments in any order with the syntax: parameterName=argument in the parameter list. See KeywordArgumentsDemo.py Winter 2016
5
CISC101 - Prof. McLeod5 Keyword Arguments, Cont. All positional arguments must come before keyword arguments. But, the keyword arguments can be in any order. Unless the function has default arguments, you must still supply arguments for each parameter. Style note: No spaces on either side of the equals sign. Winter 2016
6
CISC101 - Prof. McLeod6 Default Arguments You can make a function a great deal more flexible by making it optional for the user to supply all the arguments. You do this by creating default arguments in your function definition statement. Default arguments must come after all positional parameters. The same syntax as for Keyword Arguments, but this time it is used in the def line instead of the invoking line. See DefaultArgumentsDemo.py Winter 2016
7
CISC101 - Prof. McLeod7 Default Arguments, Cont. You must decide which parameters are optional, if any. Then you must make assumptions to come up with values for those optional parameters. Supplying an argument value for a default argument replaces the default value. We will see quite a bit of this stuff when using Tkinter! Reduces the need for multiple function versions. Winter 2016
8
Example: The print() BIF We know that print() can have any number of positional arguments. It also has sep= and end= default arguments. They have been defaulted as sep=" " and end="\n" In other words the default separator between printed items is a space and a linefeed will be added to the output after the last item has been printed. You can only change these values using the keyword arguments. CISC101 - Prof. McLeod8Winter 2016
9
Variable Length Parameter Lists How can print() accept any number of positional arguments? See ConcatenateDemo.py So, in the function, the *args parameter is an iterable containing all positional arguments. It will not include keyword arguments. Winter 2016CISC101 - Prof. McLeod9
10
Example: Our Robust Input Function So far, our input function has to have two limits defined for the legal low and legal high limits for the desired number. Suppose there is no upper or lower limit – how can you use this function? Set the two limits to some default value – but what should that value be? Your only choice is to set them to None. (This keyword can be used to create variables without actually assigning them a value.) See myInputAgain.py again and testMyInput.py. CISC101 - Prof. McLeod10Winter 2016
11
Our Robust Input Function, Cont. Note that we could not use a variable set to None in a comparison (or in an expression that carries out a calculation). So, we had to alter the code in the function. Is it worth it? Only two things left to fix: –What if the invoking code supplies a string for lowLimit or highLimit? –What if the invoking code supplies a lowLimit > highLimit? CISC101 - Prof. McLeod11Winter 2016
12
Checking Argument Types A disadvantage of loosely typed language like Python is that the interpreter cannot check to make sure that you are supplying the proper argument types to a function. Improper argument types will result in a run-time error unless the function itself checks. What should the function do if argument types are not correct? CISC101 - Prof. McLeod12Winter 2016
13
Checking Argument Types, Cont. A function that expects a number for an argument might get a string (or something else!) instead. Ouch! So, a *very* robust function will check argument types. For example if numArg is supposed to be a number, check to see if type(numArg) == type(1) or type(numArg) == type(1.1) is True. CISC101 - Prof. McLeod13Winter 2016
14
Most! Robust Input Function Or, you could just have used: type(numArg) == int or type(numArg) == float Other types: str, list, set, etc… See the mrInput module, and the program testMrInput.py. If the lowLimit and highLimit values are reversed then just switch them. What to do if they are the same? CISC101 - Prof. McLeod14Winter 2016
15
isinstance() Another BIF that you can use to check types. For example: >>> aVal = 1234 >>> isinstance(aVal, int) True >>> isinstance(aVal, str) False Winter 2016CISC101 - Prof. McLeod15
16
Summary Keyword arguments allow you some flexibility in how you supply arguments when invoking a function. If you have a variable number of parameters, then you have to use keyword args (like for print()). Default arguments are used in a function definition to assign default values to parameters that do not have to be mapped when the function is invoked. The *param usage allows you to have variable length paramter lists. CISC101 - Prof. McLeod16Winter 2016
17
Summary, Cont. Other languages (like Java, but not C++) have to use method overloading to provide the flexibility of default arguments. The keyword None can be used to assign a default value to arguments when you cannot decide on a sensible value but still want that parameter to be optional. You should check argument types in a Python function. Use the type() or isinstance() BIFs. CISC101 - Prof. McLeod17Winter 2016
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.