Calling Methods the Object-Oriented Way

Because every method in class str requires a string as the first argument (and, more generally, because every method in any class requires an object of that class as the first argument), Python provides a shorthand form for calling a method where the object appears first and then the method call:

 >>>​​ ​​'browning'​​.capitalize()
 'Browning'
 >>>​​ ​​'Sonnet 43'​​.center(26)
 ' Sonnet 43 '
 >>>​​ ​​'How do I love thee? Let me count the ways.'​​.count(​​'the'​​)
 2

When Python encounters one of these method calls, it translates it to the more long-winded form. We will use this shorthand form throughout the rest of the book.

The help documentation for methods uses this form. Here is the help for method lower in class str. (Notice that we can get help for a single method by prefixing it with the class it belongs to.)

 >>>​​ ​​help(str.lower)
 Help on method_descriptor:
 
 lower(...)
  S.lower() -> str
 
  Return a copy of the string S converted to lowercase.

Contrast that documentation with the help for function sqrt in module math:

 >>>​​ ​​import​​ ​​math
 >>>​​ ​​help(math.sqrt)
 Help on built-in function sqrt in module math:
 
 sqrt(...)
  sqrt(x)
 
  Return the square root of x.

The help for str.lower shows that you need to prefix the call with the string value S; the help for math.sqrt doesn’t show any such prefix.

The general form of a method call is as follows:

 expression.method_name(arguments)

So far every example we’ve seen has a single object as the expression, but any expression can be used as long as it evaluates to the correct type. Here’s an example:

 >>>​​ ​​(​​'TTA'​​ ​​+​​ ​​'G'​​ ​​*​​ ​​3).count(​​'T'​​)
 2

The expression (’TTA’ + ’G’ * 3) evaluates to the DNA sequence ’TTAGGG’, and that is the object that is used in the call on string method count.

Here are the steps for executing a method call. These steps are quite similar to those for executing a function call in Tracing Function Calls in the Memory Model.

  1. Evaluate «expression»; this may be something simple, like ’Elizabeth Barrett Browning’ (a poet from the 1800s), or it may be more complicated, like (’TTA’ + ’G’ * 3). Either way, a single object is produced, and that will be the object we are interacting with during the method call.

  2. Now that we have an object, evaluate the method arguments left to right. In our DNA example, the argument is ’T’.

  3. Pass the result of evaluating the initial expression as the first argument, and also pass the argument values from the previous step, into the method. In our DNA example, our code is equivalent to str.count(’TTAGGG’, ’T’).

  4. Execute the method.

When the method call finishes, it produces a value. In our DNA example, str.count(’TTAGGG’, ’T’) returns the number of times ’T’ occurs in ’TTAGGG’, which is 2.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.29.119