1 / 60

Learning to Program With Python

Learning to Program With Python. Advanced Class 9. Topics: Miscellaneous Day. The ‘is’ operator The keyword ‘del ’ Ternary expressions Truthiness and mutability in default arguments Exceptions List comprehensions Lambdas and function programming Function decorators Splat operators

archie
Download Presentation

Learning to Program With Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Learning to ProgramWith Python Advanced Class 9

  2. Topics: Miscellaneous Day • The ‘is’ operator • The keyword ‘del’ • Ternary expressions • Truthiness and mutability in default arguments • Exceptions • List comprehensions • Lambdas and function programming • Function decorators • Splat operators • Monkey patching

  3. The ‘is’ operator • A common mistake is to confuse the ‘is’ operator with the ‘==’ operator. • Like the ‘==’ operator, the ‘is’ operator forms a boolean expression by comparing two objects and returning True or False.

  4. The ‘is’ operator • Whereas the ‘==’ operator compares values, the ‘is’ operator compares identities. • Two things are identical if they are literally the same thing in the memory of the computer. • As a metaphor, human twins are equal but not identical. Only a single, individual person is identical to themselves.

  5. The ‘is’ operator >>> m = [1, 2, 3] >>> x = [1, 2, 3] >>> m == x True >>> m is x False • They are separate lists with equal values.

  6. When to use the ‘is’ operator • Pretty much never. • Okay, there are situations where it’s appropriate, but they don’t really come up until you’re doing some very high-level programming. Personally I’ve never needed it. • Just always use ‘==’. If you see someone using ‘is’ to compare strings, ints, bools, lists, and so on– there’s a good chance they don’t actually understand the difference.

  7. Final notes on ‘is’; the id() function • The built-in id() function returns an objects id in the memory of the computer. No two objects can have the same id– that would be analogous to two people occupying the same physical space simultaneously. • The following two boolean expressions are synonymous: >>> id(myObject) == id(myObject) True >>> myObject is myObject True

  8. Final notes on ‘is’; string interning • You will observe interesting and seemingly inexplicable behavior with ints and strings, where things are identical that you wouldn’t expect to be. • Read up on string interning, and be aware that a semi-similar principle applies to ints and floats up to a certain size.

  9. The del operator • It deletes a variable– the variable’s name becomes meaningless and the memory is freed up. • It can also be used to delete elements out of lists and pairs out of dictionaries.

  10. The del operator >>> m = 5 >>> m 5 >>> del m >>> m Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> m NameError: name 'm' is not defined

  11. The del operator >>> m = [1, 2, 3, 4, 5, 6] >>> del m[2] #deletes the item at index 2 >>> m [1, 2, 4, 5, 6] >>> del m[:3] #deletes the slice up to index 3 >>> m [5, 6] >>>

  12. The del operator #Remember, dictionaries hold key-value pairs. >>> m = {‘a’ : 1, ‘b’ : 2, ‘c’ : 4} >>> del m[‘b’] #deletes the key and its value >>> m {‘a’ : 1, ‘c’ : 4} • You can’t delete the value alone, and deleting only the key wouldn’t even make any logical sense and likewise cannot be done. How would you access a value with no key…? • By deleting the key you delete both.

  13. Ternary expressions • It’s exactly like being able to embed an if/else clause inside of a value. It looks like this: review = “great” if movie == “Jurassic Park” else “terrible” • Depending on the result of the string comparison, the variable ‘review’ will be assigned one of two values. • This allows you to conditionally set values on a single line of code.

  14. Ternary expressions • The result of the preceding example is identical to this: if movie == “Jurassic Park”: review = “great” else: review = “terrible”

  15. The ternary operator • A ternary expressions is an expression, surprisingly. The whole thing has a value,which will be determined after the expression is evaluated, just like any other expression. • Whereas if/else statements can determine whether to execute statements and entire blocks of code, a ternary expression is simply used to decide what a given value will be. • If/else statements are more powerful, obviously. They can do the job of the ternary expression, whereas the reverse is not true. The ternary expression is just syntactic sugar.

  16. Truthiness • In programming, truthiness is the idea of whether a given value should be considered ‘True’ or ‘False’. • If you place a given value where a boolean expression is expected, Python will coerce the value into a boolean….it will convert it.

  17. What does that mean? • In python, everything is True except the following: • empty string • zero (int or float) • empty list, tuple, dict, or set • None • This allows you to predict the result of that implicit conversion and use it to your advantage in writing more concise code.

  18. Truthiness with lists, tuples, dicts, etc • It is correct but not ‘pythonic’ to write the following: if len(myList) > 0: #do something • It is both correct and python to write this instead: if myList: #do something

  19. Truthiness with strings • It is correct but not ‘pythonic’ to write the following: if len(myString) > 0: #do something • It is both correct and python to write this instead: if myString: #do something

  20. Truthiness with None • It’s fairly common to give a variable or argument a default value of None and then check it later to see if it was given a proper value or if for whatever reason it remained None. var = None #do stuff if var: #do work with var

  21. __bool__ • This is the name of the magic method that tells Python how to evaluate an instance of your class if it is used where a boolean is expected. Obviously it must return True or False depending on whatever. • With the Deck class from a previous lecture, __bool__ would return False if the Deck’s self.cards property contained an empty list, and True otherwise.

  22. Mutability and default arguments • This is a common pitfall with python beginners. who set an empty list as a default value to an argument of a function. Observe: def f(arg=[]): arg.append(5) return arg #(example continues onto next slide)

  23. Mutability and default arguments >>> f() [5] >>> f() [5, 5] #wait, WHAT? >>> f() [5, 5, 5] #OH MY GOD WHAT’S HAPPENING

  24. Initialization of default arguments • Default arguments are evaluated once, at the time of function definition. • They are notevaluated anew each time you call the function. • This means that arg began as an empty list, but each time you call the function f, arg has an extra 5 appended to it.

  25. Mutability at play again • This only happens with making changes to mutable default arguments, such as lists, sets, dictionaries, and instances of user-defined classes. • Reassigning an argument’s value has zero effect, because we’re not changing the existing value, we’re creating a new one. The next time the function is called, the argument name gets pointed back at its proper original value.

  26. Reassigning arguments • So this will not error….the second time you call it, it’s not trying to append 3 to an int. arg becomes a list again the next time you call it. Only the append operation carries over to subsequent calls. def f(arg=[]): arg.append(3) arg = 20 return arg

  27. Solution: use None • Always use None as a default value where you want an empty list/dict/set/etc, then check it and reassign it inside the body of the function. def f(arg=None): if not arg: arg = [] • If an argument is provided, arg will not be None, and the if statement will pass over it, so it behaves as intended.

  28. Using None as a default value • Since its default value is the immutable None, arg’s default value never changes, and it always becomes an empty list if no argument is provided. • So you get your default empty list, and subsequent function calls don’t get messed up.

  29. Exceptions • I’m not going to go too in depth here, just a cursory introduction. • An exception can be thought of as an error. • There are special keywords in most programming languages to throw exceptions and to catch those exceptions without the program crashing.

  30. Raising exceptions • The keyword for this is ‘raise’. defconvertDigitToWord(digit): if not 0 <= digit <= 9: raise ValueError(“Invalid argument.”) return [“zero”, “one”, “two”, “three”, “four”, “five”, “six”, “seven”, “eight”, “nine”][digit]

  31. Errors are exceptions • I know I’m talking about raising exceptions and then I just wrote ‘raise ValueError’, but it doesn’t matter. Like I said– exceptions are errors. • The only kind of error that isn’t considered an exception is a Syntax Error, in which case your program just insta-crashes. There’s no saving a syntax error.

  32. ValueError • This is one of the many built-in types of exception….it should be used when an operation or function receives an argument that is the right type but has an inappropriate value. • The text in between parentheses is the message that gets printed out in the shell if the exception is not caught and repressed by other code.

  33. Other built-in exception types • IndexError: for when you try to access an index that does not exist on a list, dict, tuple, user-defined class, or other such object. • AttributeError: usually for when you try to access an object attribute that doesn’t exist. • ImportError: for when an import statement cannot find the specified module.

  34. Other built-in exception types • A complete listing can be found here: http://docs.python.org/3.3/library/exceptions.html#exceptions.Exception

  35. Catching exceptions • In production code (the final version users receive!), if some function or method or operation has the possibility of raising an exception, it’s a good idea to wrap it inside what’s called a try/except clause. • This means try to do something, and if it fails and raises an exception, do this instead. • It’s almost like if/else but for catching errors.

  36. Catching exceptions • This is a try/except block….this is the syntax for dealing with the possibility of various errors. try: print( 10 / 0) except ZeroDivisionError: print(“mathematically impossible!”)

  37. Catching exceptions • You can have multiple except clauses to deal with multiple possible types of exceptions: try: print(a / b) #assume a and b are given values except TypeError: print(“Can’t divide those two types!”) except ZeroDivisionError: print(“Can’t divide by zero!”)

  38. Catching exceptions • Likewise, a single except clause can take a parenthesized tuple of multiple exception types, if you want to deal with them the same way. try: print (a, b) except (ZeroDivisionError, TypeError): print(“Something went wrong.”)

  39. The else clause with exceptions • An else clause after a try/except block is executed only if no exception was raised. You can only have one else clause with try/except blocks, and it must follow all the except clauses. try: print( a / b) except ZeroDivisionError: print(“Can’t divide by zero, stop everything.”) else: print(“Nothing went wrong, do something else!”)

  40. List Comprehensions • My favorite part of python– a fast and simple way of populating lists. • Here’s the instantiation of a list of ints from 0 to 999. L = [n for n in range(1000)]

  41. List comprehension syntax [resultant_value for item in iterable] So in our previous example, for every number in the range 0 to 999, we simply stuck that number in our list. [n for n in range(1000)] #evaluates to a list For n in range(1000), put n in the resulting list.

  42. List comprehensions Here are some examples of transformations we can put the list through to get all sorts of results: [2*n for n in range(5)]  [0, 2, 4, 6, 8, 10] [-i for i in range(5)]  [0, -1, -2, -3, -4, -5] [str(num) for num in range(2)]  [‘0’, ‘1’]

  43. List comprehensions • Of course, we don’t have to use the range function– it just comes in handy. We can use anything iterable! [c for c in “hello”]  [‘h’, ‘e’, ‘l’, ‘l’, ‘o’] [c + “cow” for x in “cat”]  [‘ccat’, ‘acow’, ‘tcow’] [e ** 2 for e in [4, 2, 6] ]  [16, 4, 36]

  44. List comprehensions • We don’t even have to use the item we’re plucking from the iterable. In these examples the only thing we’re really using from the iterable is its length, to repeat a constant result that many times. [“meow” for x in range(3)]  [‘meow’, ‘meow’, ‘meow’] [2 for x in ‘optimus’]  [2, 2, 2, 2, 2, 2, 2]

  45. List comprehensions • Here’s a list of 400 random letters: import random as R, string as S m = [R.choice(S.ascii_lowercase) for x in range(400)] • I gave the random module the name R and the string module the name S, then used the random.choice method to randomly choose letters from a string of the alphabet, which is what’s returned by S.ascii.lowercase.

  46. Filtering list comprehensions • You can tell the comprehension to only include elements that pass a specified test. #only even numbers m = [n for n in range(100) if n % 2 == 0] #only odd numbers m = [n for n in range(100) if n %2 != 0]

  47. Filtering list comprehensions • Don’t confuse that syntax with the similar-looking ternary expression…using the filtering option on list comprehensions only allows you to omit things. There is no else clause. • ALTHOUGH, you could use a ternary expression as PART of the list comprehension, if you really want to be cool. See next slide.

  48. List comprehension + ternary m = [n if n % 2 == 0 else “odd” for n in range(5)]  [ 0, ‘odd’, 2, ‘odd’, 4 ] • This is not the comprehension filter that goes at theend . . . this is a ternary expression at the beginning.

  49. Dictionary and set comprehensions • They exist. Go read about them. • Time constraints… • There are no tuple comprehensions, but you could just do this: m = tuple([n for n in range(10)])

  50. Stuff relating to functions. • I had originally put some very precise terminology right about here, but then I started reading things on the internet and discovered I was slightly mistaken about something, and then the explanations overlapped and were vague, and I lost interest. • We’re just going to be practical and not worry too much about exact terminology, but for the hell of it I’m going to call this stuff “functional programming.”

More Related