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
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. |
Method invocation uses dot notation, while function-call syntax places the object inside the parentheses.
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.
- 1Read the object
word refers to the original string banana.
- 2Invoke the method
The dot connects word to upper, and the parentheses invoke upper on that string.
- 3Produce the result
upper returns a new string with all letters converted to uppercase.
- 4Store the result
The assignment places the returned string in
new_word. - 5Check 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()- 1The first expression has the object, dot, and method name, but no parentheses. It refers to the method rather than running it.
- 2The second expression includes parentheses. Those parentheses signal that the method is being called.
- References the upper method without invoking it.
- Invokes the upper method on word.
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
Common mistake Forgetting the dot
What it looks likeword upper()Why it failsThe dot is the connection between the string object and its method. Without it, the expression does not use method-invocation syntax.
Do this insteadWrite word.upper().Common mistake Omitting the parentheses
What it looks likeword.upper
Why it failsWithout parentheses, the method is referenced but not invoked.
Do this insteadWrite word.upper() when the goal is to run the method.
Common mistake Failing to assign the returned result
What it looks likeword.upper()Why it failsThe method produces a new uppercase string, but the original string remains unchanged. Without assignment, the returned result is not captured for later use.
Do this insteadAssign 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.
- 1Inspect the object
word identifies the string object.
- 2Inspect the connection
The dot connects word to upper.
- 3Inspect invocation
The parentheses are missing, so upper is referenced rather than invoked.
- 4Inspect 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
- 1A method invocation follows the pattern
object.method_name(). - 2The dot connects an object to its method, and parentheses signal invocation.
- 3For strings, upper returns a new uppercase string and does not modify the original string.
- 4Assign the returned value when you want to use it later.
- 5Check the dot, method name, parentheses, and assignment when debugging a method call.
At a glance
- 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.