Concepts / Introduction to Python Objects

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.

  • Programming

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.

String objectone entityCharacter datathe textMethod toolkitbuilt-in functionscontainsbundles
What does a string object contain besides its character data, and how are its methods bundled with it?

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.

Worked example

Tracing upper on a string

Consider the expression greeting.upper(). What role does each part play?

  1. 1
    Find the object

    greeting identifies the string object whose character data will be used.

  2. 2
    Find the method

    upper is the built-in method that converts the string's characters to uppercase.

  3. 3
    Call the method

    The parentheses show that upper is being executed rather than merely referred to.

  4. 4
    Receive the result

    The method processes greeting's data and returns a new uppercase result.

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.

greetingstring object.connects objectuppermethod name()calls methodfollowed bylocatesexecuted by
How does Python use dot notation to move from a string instance to one of its available methods?
Key point

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.

greetingone string's dataupper()shared methoduppercase resultbased on greetingmessageanother string's datauppercase resultbased on messagecallscallsreturnsreturns
How can the same built-in method be available to many string instances while producing results based on each instance's data?
In the real world

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 like

    Thinking of upper() only as a general operation instead of as the upper method belonging to a particular string

    Why it fails

    A method belongs to an object and operates on that object's data.

    Do this instead

    Read 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 like

    Writing the string object and method name without the connection provided by dot notation

    Why it fails

    The dot tells Python to look for the method as something belonging to the object.

    Do this instead

    Use the object.method() structure when calling a method.

  • Common mistake

    Leaving out the parentheses when intending to run the method

    What it looks like

    Referring to upper rather than calling upper()

    Why it fails

    Without parentheses, the method is being referred to rather than executed.

    Do this instead

    Include parentheses when the goal is to call the method.

  • Common mistake

    Expecting a string method to change the original string

    What it looks like

    Assuming that calling upper() modifies the existing string object

    Why it fails

    String methods return a new result, and strings are immutable in Python.

    Do this instead

    Treat 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?

  • It uses the calling string's data

  • It operates independently of the string object

Reveal answer
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.

EASY

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

  1. 1
    A string is a Python object that bundles character data with built-in methods.
  2. 2
    A method is a function that belongs to an object and operates on that object's data.
  3. 3
    Dot notation connects an object to a method, as in greeting.upper().
  4. 4
    Parentheses indicate that the method is being called.
  5. 5
    String 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.

More concepts starting with I