Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Attributes in Mutable Objects

00:00 Another way Python programmers implement pass by reference is by using object attributes. But first, a definition. A Python object is considered mutable if it can be changed in place without being reassigned.

00:15 There aren’t many of these in Python. Objects of the collection types list, and set, and dictionaries are mutable. And objects of programmer-created classes are mutable as well. If you’re not mutable, then you’re immutable.

00:31 Just about everything you’ve seen in this course is immutable. Objects that represent numbers, Boolean values, individual characters, and Unicode are all immutable, as are strings and tuples.

00:46 But here, we’re focusing on mutable objects. Since an object variable and the parameter it’s passed to are bound to the same object, a change made inside the function to that object through its parameter reference will be reflected by the argument variable after the function ends.

01:07 So basically, you can perform pass by reference if your arguments are mutable objects. In this lesson, you’ll see an example where you create your own custom objects, and in the next lesson, you’ll look at modifying collection objects using pass by reference.

01:27 So, if you want a function to directly modify an argument, make the argument of some custom class, then allow the function to modify one of its attributes.

01:40 Here’s a new version of the square() function, one that expects the object passed to have an attribute .n, and it will square that attribute’s value and save it back to that attribute, just like the C++ function did with the parameter num.

01:57 To use it, create an object with an attribute .n, where that attribute holds the value you want to pass by reference. So again, this version of the square() function takes an argument which will be bound to the parameter name instance, named like this since it’s an instance of some class.

02:18 The only requirement is that the object have an attribute .n, because this function will take that attribute’s value, multiply it by itself, and reassign it back to that attribute.

02:33 It doesn’t return anything since the side effect of modifying .n was the purpose of this function. To test it, you need to define a class, then create an object of that class.

02:46 Well, for a class that doesn’t really need any methods and is only going to have attributes you create as needed, you can use Python’s SimpleNamespace type. It’s basically an empty class definition.

02:59 You make an object with that class and then create whatever attributes it needs. It’s just as if you created a class and then just wrote pass after the class header.

03:09 So SimpleNamespace is exactly what we want: a class to create objects, give an attribute, and use it. So, we will create an object of the SimpleNamespace class, give it an attribute .n and assign it a value of 4, then call this version of the square() function, passing that object as the argument. When I run this script, after performing square(), you will see that the attribute .n will have indeed be changed to 16.

03:46 I’ll let ptpython run the script when I import it, because I want to use this function again interactively.

03:53 So, the script will run, the print statement at the very end will display, and we should see that the attribute .n has been changed to 16.

04:02 And it has. Note, this only works with mutable objects.

04:10 So, suppose I create a namedtuple object with an attribute .n. So my namedtuple type will be called NT.

04:23 That’s the name, and it will have an attribute .n. Then create an object whose .n attribute value is 4.

04:37 See? It’s 4. But watch what happens when I try to use this version of square() on that object. nt has an attribute .n, but it’s a tuple. It’s an immutable type.

04:53 You can’t reassign any of its attributes to a new value. Something to keep in mind: Changing an attribute of an object doesn’t affect class attributes.

05:10 So if I define a simple class, CL for class, give it a class attribute n, and then create an object of that class,

05:25 there’s the object showing the class attribute .n. But then if I call the function that modifies that attribute, the value of that attribute for this object is 16 but the class attribute is still 4.

05:43 Next, you’ll see how dictionaries and lists can be modified using pass by reference.

Become a Member to join the conversation.