13.3. Working With Objects

Once created, objects are mutable. We can change the values of properties or add new ones as needed.

13.3.1. Object Properties

When using objects, we often want to access the value of a property, change a property value, or add a completely new property. To do this, we need the object’s name and the property name.

To modify object properties, we use dot notation:

object_name.property_name  # Access & return a property value from object_name.

object_name.property_name = new_value  # Change the value of an existing property.

object_name.new_property = value   # Add a new property and value to the object.

Note that property_name and new_property do NOT end with a set of parentheses ().

Try It!

In the program below, dog is an object with two properties—name and age. Follow the instructions below the editor to practice using dot notation with an object.

  1. Run the program as-is to see how the dot notation on lines 8 and 10 accesses and returns the property values.

  2. On line 13, use dot notation to change the value of the name property to 'Fleas'.

  3. On line 14, use the += operator to increase the age of dog by 3 years.

  4. Run the program again. Properly done, the output should look like:

    Spot
    3
    Fleas 6
    
  5. Uncomment line 19 and run the program. You should see an error message because dog does not contain a property called breed.

  6. On the line 17, use dot notation to create a new property called breed and assign it a string value.

  7. On line 18, add the is_cute property to dog. Assign it a boolean value instead of a string.

  8. Run the program again. Properly done, the output should look similar to:

    Spot
    3
    Fleas 6
    mutt True
    

Tip

Object properties work like the key/value pairs in a dictionary. Each property name acts as a label that points to a specific piece of data (the value).

13.3.2. Calling Methods

When we learned about strings, lists, and dictionaries, we practiced calling methods on each type of object. The same syntax applies for all other objects as well:

object_name.method_name(arguments)

Note that some methods require arguments (data) inside the parentheses, while other methods do not.

my_list.append(100)  # 100 is the argument for the list method .append()

my_string.lower() # The string method .lower() requires no arguments

13.3.3. Methods vs. Functions

Back in the functions chapter, we learned that these defined blocks of code behave like a machine when called. A function takes a set of input, performs an action with it, and in some cases returns an output.

Methods are also defined blocks of code that perform actions. Just like functions, we can call methods over and over again, send them input, and collect an output. So what’s the difference between methods and functions?

Functionally (pun intended), the two are the same thing. However, we should consider a method as a special type of function, one with some extra restrictions and advantages.

13.3.3.1. Call Syntax

The first difference involves the syntax for calling functions vs. methods.

function_name(arguments)

object_name.method_name(arguments)

As programmers, we call functions and send them all the data they need. If we leave out a value, our program will likely crash.

On the other hand, when we use an object to call a method, the object can supply some or all of the required data.

13.3.3.2. Methods are Object Specific

We can call a function anywhere we want in our program, and it will often work with different data types. For example, len() operates equally well on the string, list, and dict data types. The max() function operates on lists, strings, and a set of numbers separated by commas.

Methods are defined to work with one specific type of object. We cannot call a method on objects of a different type.

13.3.3.3. Try It!

In this example, the dog object includes three methods called speak, fetch, and increase_age.

  1. On line 9, add a statement to call the speak method, then run the program. The method requires no argument, but including an integer changes the output. Try it!
  2. The speak method prints to the console, but the fetch method does not. On line 12, print the value returned by fetch.
  3. Try adding a string argument when you call the fetch method.
  4. On line 15, print the age property for dog.
  5. On line 16, call the increase_age method, which requires an integer as an argument. Print the age property again to check the result.