Understanding Functions and Their Syntax
Method invocation uses dot notation: object.method_name() instead of function syntax like function_name(object).
A String Method in Action
A method invocation is a call that uses an object followed by a dot and a method name. For a string, the pattern is object.method_name(). The object supplies the data that the method operates on, and the parentheses indicate that the method is being invoked.
What do you think happens?
What value will new_word contain after these statements?
Reveal answer
Answer: BANANA
The upper method reads the string banana, creates a new string with all letters converted to uppercase, and returns that new string. The assignment stores the returned string in new_word.
BANANAThe Three Parts of a Method Call
The expression word.upper() contains three essential pieces. word is the string object. The dot connects that object to its method. upper is the method name, and the parentheses invoke the method. Reading the expression from left to right gives a useful trace: start with word, connect it to upper, then run upper on the string.
| Syntax form | Where the object appears | Meaning |
|---|---|---|
| object.method_name() | Before the dot | The method is invoked on the object. |
| function_name(object) | Inside the parentheses | The object is passed to a standalone function-style call. |
The two forms place the object in different locations. In object.method_name(), the dot explicitly connects the object to the method. In function_name(object), the object appears as an argument inside the parentheses. This article focuses on the method-invocation form used with strings.
From String to Returned Value
When word.upper() is invoked, the string stored in word flows into the method. The method reads the original string, performs the uppercase operation, and returns a new string. The original string remains unchanged. Assignment then gives the returned string a name that can be used later.
Tracing word.upper()
Determine the value of new_word and the value of word after the method invocation.
Read the object: word refers to the original string banana.
Invoke the method: The dot connects word to upper, and the parentheses invoke upper on that string.
Produce the result: upper returns a new string with all letters converted to uppercase.
Store the result: The assignment places the returned string in new_word.
Check the original: The original string is not modified, so word still contains banana.
new_word contains BANANA, while word still contains banana.
Reference or Invocation
word.upper word.upper()
This distinction matters because referring to a method and invoking a method are different actions. The dot identifies the method attached to the object. The parentheses trigger the execution. When tracing a call, check for both parts instead of treating the method name alone as the operation.
Debugging Method Syntax
Forgetting the dot
The dot is the connection between the string object and its method. Without it, the expression does not use method-invocation syntax.
Fix:
Write word.upper().Omitting the parentheses
Without parentheses, the method is referenced but not invoked.
Fix:
Write word.upper() when the goal is to run the method.Failing to assign the returned result
The method produces a new uppercase string, but the original string remains unchanged. Without assignment, the returned result is not captured for later use.
Fix:
Assign the result when it is needed later, as in new_word = word.upper().
Debug a method call from left to right. First identify the object, then check that a dot connects it to the method name, then check that parentheses invoke the method. Finally, ask whether the returned result needs to be assigned. This sequence checks the structure of the call and the handling of its result separately.
Practice the Trace
Trace this method invocation mentally. Identify the object, the method, the returned value, and the value that remains in the original object name: word = 'banana'; new_word = word.upper().
Hints
- The object is the name before the dot.
- The parentheses show that the method is invoked.
- The method returns a new uppercase string.
- Check whether the assignment changes word or stores the returned value in new_word.
Checking a Call Before Running It
Explain what is missing from word.upper if the goal is to invoke the method and save its result.
Inspect the object: word identifies the string object.
Inspect the connection: The dot connects word to upper.
Inspect invocation: The parentheses are missing, so upper is referenced rather than invoked.
Inspect assignment: If the returned string is needed later, the completed invocation should be assigned to a name.
Use new_word = word.upper() when the intention is to invoke upper and retain its returned string.
Key Takeaways
- A method invocation follows the pattern object.method_name().
- The dot connects an object to its method, and parentheses signal invocation.
- For strings, upper returns a new uppercase string and does not modify the original string.
- Assign the returned value when you want to use it later.
- Check the dot, method name, parentheses, and assignment when debugging a method call.
Key Takeaways
- Method invocation places the object before a dot: object.method_name().
- The dot connects the object to the method, while parentheses invoke the method.
- The string method upper returns a new uppercase string and leaves the original string unchanged.
- A method result must be assigned if it needs to be used later.
- Common syntax checks include the object, dot, method name, parentheses, and assignment.