3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] Python Type "help", "copyright", "credits" or "license" for more information. # --------- Testing our code so far # We evaluated point.py to confirm that all the it passes all the doctests. [evaluate point.py] # --------- Better help for the client code # When we have a str and we want to know what we can do with it, what do we do? s = "hello" help(s) no Python documentation found for 'hello' # We call help. help(str) Help on class str in module builtins: class str(object) | str(object='') -> str | [... More help omitted ...] | | count(...) | S.count(sub[, start[, end]]) -> int | | Return the number of non-overlapping occurrences of substring sub in | string S[start:end]. Optional arguments start and end are | interpreted as in slice notation. | [ ... ] | | upper(...) | S.upper() -> str | | Return a copy of S converted to uppercase. | [ ... ] # The help for str tells us everything we need to know to use a str. # For example, we know everything we need in order to call upper. s 'hello' s.upper() 'HELLO' # What help are we providing a client of our own class, Point? help(Point) Help on class Point in module __main__: class Point(builtins.object) | A n-dimensional point. | | Methods defined here: | | __init__(self, coords) | Correction: | (Point, list of number) -> NoneType | | Initialize this point with these coords. | We will return to this docstring to add an example | showing that p is as it should be. | | >>> p = Point([3, 4.0, 5.32, 6, 7]) | >>> p.coords | [3, 4.0, 5.32, 6, 7] | | distance_from_origin(self) | (Point) -> float | | Return the distance from this Point to the | origin. | | >>> p = Point([3, 4]) | >>> p.distance_from_origin() | 5.0 | | sing(self, punctuation) | (Point, str) -> NoneType | | A silly method used in explaining the parameter "self". | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) # Any code that wants to use class Point will be able to directly # access the instance variable coords, as shown in our doctest # for method __init__. But how would anyone know what the instance # variable is? The help above certainly doesn't tell them. # Solution: We modified the class docstring to add this information. # Now help is more helpful. :-) help(Point) Help on class Point in module __main__: class Point(builtins.object) | A n-dimensional point. | coords is a list of numbers containing the coordinates | of this Point. [ ... rest of the help omitted ... ] # --------- Understanding "self" word = 'supercalifragilisticexpialidocious' # There are built-in functions you can use on our string. len(word) 34 # There is no built-in function called count. count(word, 'i') Traceback (most recent call last): File "", line 1, in builtins.NameError: name 'count' is not defined # Isn't there something for counting occurrences of a str inside a str? # Yes! But it's defined inside the str class. # We can use this "count" if we say where to find it. str.count(word, 'i') 7 # We passed count the two things it needs to know in order to do its job: # the str to look within (word) and the str to look for within it ('i'). # But you are used to calling count in a different way: word.count('i') 7 # How did it know which string to count the 'i's in? # We still gave count the two things it needs: the str to look for # we gave via an argument. The str to look within, we gave when # we said "word." # In the header for method count, # there have to be two parameters to receive these two values. # The first parameter always receives the object # whose method is being called. # We call that parameter "self". # (We examined the silly method called sing, to review how self is used # within a method.) p = Point([5, 1, 33]) p.sing("!") 5! 1! 33! # This like saying "hey p, you're a Point, sing about yourself with enthusiasm" Point.sing(p, "!") 5! 1! 33! # That is like saying "hey Point class, you know about points. Please sing # about point p, with enhusiasm". # Note that the first parameter, self, is just a parameter. # We call it "self" by convention, because that fits how we think # about ojbects. # We could call it "toad"! # (We changed the code to use "toad" instead of "self" as the parameter name, # and observed that it still worked.) [evaluate point.py] p <__main__.Point object at 0x101458a20> p.sing("!") 5! 1! 33! # --------------- Special method __init__ # __init__ is a special method. # - we don't have to call it explicitly. It's called for use # whenever an object is created. # - we "inherit" an __init__ method even if we don't define one ourselves. # - the double underscores are used to say that this a special method. # Another special method: __str__ # - called automatically, when you print the object. p <__main__.Point object at 0x101458a20> print(p) <__main__.Point object at 0x101458a20> # That just called the __str__ method. But I didn't define one. # Still, we inherited one. It just doesn't do anything interesting. # (We overrode the inherited one, so we have a __str__ that does # something more useful.) # On our first run of the code with the new method, doctest reported errors: [evaluate point.py] ********************************************************************** File "/Applications/Wing101.app/Contents/Resources/src/debug/tserver/_sandbox.py", line ?, in __main__.Point.__str__ Failed example: print(p) Expected: (3,0, 4.0) Got: [3, 0, 4.0] ********************************************************************** File "/Applications/Wing101.app/Contents/Resources/src/debug/tserver/_sandbox.py", line ?, in __main__.Point.__str__ Failed example: p.__str__() Expected: '(3,0, 4.0)' Got: '[3, 0, 4.0]' ********************************************************************** File "/Applications/Wing101.app/Contents/Resources/src/debug/tserver/_sandbox.py", line ?, in __main__.Point.__str__ Failed example: str(p) Expected: '(3,0, 4.0)' Got: '[3, 0, 4.0]' ********************************************************************** 1 items had failures: 3 of 4 in __main__.Point.__str__ ***Test Failed*** 3 failures. # (There was a typo in the doctest. We fixed it and observed that the code ran without error.) [evaluate point.py] # --------------- Special method __repr__ # Let's write another special method: __repr__ # It's like __str__ in that it gives back a str that represents the object. # But it's not like __str__ in that the str it gives back can be evaluated # to create a new object that's equivalent. # Before we write __repr__, we need to understand eval. # Anything you can type in the shell, you can eval. 33 + 99 132 eval('33 + 99') 132 max(3, 17, -1, 4) 17 eval('max(3, 17, -1, 4)') 17 # Now that we understand eval, the doctest for method __repr__ should make sense. # In the next lecture, we'll write a body for __repr__ that can pass the doctest.