Introduction to Python Objects
The parentheses matter. With parentheses, Python calls or executes the method. Without them, you are referring to the method itself rather than running it.
A String as a Bundle
When you create a string in Python, you are doing more than storing a sequence of characters. You are creating an object: a single entity that bundles the characters with built-in functions that know how to work with them. These built-in functions are called methods.
The character data distinguishes one string from another. The methods form a toolkit of operations that make sense for strings. This is an example of bundling data together with the operations that work on that data, a core principle of object-oriented programming.
Following a Method Call
A method is a function that belongs to an object. When a method is called on a string, it receives that string's data, processes the data according to its built-in logic, and returns a new result. The method therefore connects a general operation with the particular characters stored in the object that calls it.
Tracing upper on a string
Consider the expression greeting.upper(). What role does each part play?
- 1Find the object
greeting identifies the string object whose character data will be used.
- 2Find the method
upper is the built-in method that converts the string's characters to uppercase.
- 3Call the method
The parentheses show that upper is being executed rather than merely referred to.
- 4Receive the result
The method processes greeting's data and returns a new uppercase result.
The expression asks the string object stored in greeting to run its upper method on its own character data.
Reading Dot Notation
Dot notation is the syntax that connects an object to one of its methods. In greeting.upper(), greeting is the object, the dot tells Python to look for something belonging to that object, and upper() identifies and calls the method.
The parentheses matter. With parentheses, Python calls or executes the method. Without them, you are referring to the method itself rather than running it.
One Toolkit, Many Strings
Every string object has the same set of methods available, but each method operates on the data of the specific string that calls it. The method is shared as part of the string toolkit; the result depends on the individual string's characters.
Suppose two different string objects each call upper(). They use the same built-in operation, but each operation works with the characters in the string that called it. The method does not need to be rewritten for each string because it is already bundled into the string object's available toolkit.
Mistakes with Methods
Common mistake Treating a method as a separate function with no connection to the object
What it looks likeThinking of upper() only as a general operation instead of as the upper method belonging to a particular string
Why it failsA method belongs to an object and operates on that object's data.
Do this insteadRead the expression from left to right: identify the string object, follow the dot, and then identify the method that acts on that string.
Common mistake Leaving out the dot
What it looks likeWriting the string object and method name without the connection provided by dot notation
Why it failsThe dot tells Python to look for the method as something belonging to the object.
Do this insteadUse the object.method() structure when calling a method.
Common mistake Leaving out the parentheses when intending to run the method
What it looks likeReferring to upper rather than calling upper()
Why it failsWithout parentheses, the method is being referred to rather than executed.
Do this insteadInclude parentheses when the goal is to call the method.
Common mistake Expecting a string method to change the original string
What it looks likeAssuming that calling upper() modifies the existing string object
Why it failsString methods return a new result, and strings are immutable in Python.
Do this insteadTreat the method call as producing a new result while the original string remains unchanged.
Check Your Understanding
What do you think happens?
Before reading the explanation, predict what happens conceptually when a string object calls upper(). Does the method use the string's own data, or does it operate independently of the object?
Reveal answer
It uses the calling string's data.
A method belongs to an object and receives that object's data. The same upper method can be available to every string, while each call produces a result based on the particular string that called it.
Explain the parts of greeting.upper() in your own words. Identify the object, the method, the role of the dot, and the role of the parentheses. Then explain why the original string is not changed by the method call.
Hints
- Start with the name before the dot.
- The dot connects the object to something that belongs to it.
- The parentheses show that the method is being called.
- Remember that string methods return a new result.
Key Takeaways
- 1A string is a Python object that bundles character data with built-in methods.
- 2A method is a function that belongs to an object and operates on that object's data.
- 3Dot notation connects an object to a method, as in
greeting.upper(). - 4Parentheses indicate that the method is being called.
- 5String methods return new results; they do not change the original string because strings are immutable.
At a glance
- Strings bundle character data and methods into one Python object.
- Methods are object-owned functions that work with the data of the object that calls them.
- Dot notation identifies the object and the method, while parentheses execute the method.
- The same method can be available to many string objects and produce results tailored to each string.
- String methods return new results rather than changing the original string.