Passing a List as an argument to a Class in Python

avatar

Borislav Hadzhiev

Last updated: Sep 15, 2022

banner

Photo from Unsplash

Passing a List as an argument to a Class in Python #

To pass a list as an argument to a class:

  1. Pass the list as an argument when instantiating the class.
  2. Assign the list to an instance variable in the __init__() method.
  3. You can access the list on instances of the class.
main.py
class Employee(): def __init__(self, name, tasks): self.name = name self.tasks = tasks def add_tasks(self, task): self.tasks.append(task) return self.tasks bob = Employee('Bobbyhadz', ['develop', 'test']) print(bob.tasks) # 👉️ ['develop', 'test'] bob.add_tasks('ship') print(bob.tasks) # 👉️ ['develop', 'test', 'ship']

The __init__() method of the class takes a tasks list.

This argument should be passed any time we instantiate the Employee class.

The variables we declared in the __init__() method are called instance variables.

Instance variables are unique to each instance you create by instantiating the class.

main.py
class Employee(): def __init__(self, name, tasks): self.name = name self.tasks = tasks def add_tasks(self, task): self.tasks.append(task) return self.tasks alice = Employee('Alice', ['design', 'ship']) print(alice.tasks) # 👉️ ['design', 'ship'] bob = Employee('Bobbyhadz', ['develop', 'test']) print(bob.tasks) # 👉️ ['develop', 'test']

Note arguments of type list are passed by reference and not by value.

main.py
class Employee(): def __init__(self, name, tasks): self.name = name self.tasks = tasks def add_tasks(self, task): self.tasks.append(task) return self.tasks list_of_tasks = ['design', 'test'] alice = Employee('Alice', list_of_tasks) bob = Employee('Bobbyhadz', list_of_tasks) # 👇️ add item to the list of one instance alice.add_tasks('ship') print(alice.tasks) # 👉️ ['design', 'test', 'ship'] print(bob.tasks) # 👉️ ['design', 'test', 'ship']
The list we passed when creating the two instances refers to the same object. The object points to the same location in memory.

When we added a task to the list using one instance, the change got reflected in the other instance.

One way to get around this is to create a shallow copy of the list when passing it as an argument to the class.

main.py
class Employee(): def __init__(self, name, tasks): self.name = name self.tasks = tasks def add_tasks(self, task): self.tasks.append(task) return self.tasks list_of_tasks = ['design', 'test'] alice = Employee('Alice', list_of_tasks.copy()) bob = Employee('Bobbyhadz', list_of_tasks.copy()) alice.add_tasks('ship') print(alice.tasks) # 👉️ ['design', 'test', 'ship'] print(bob.tasks) # 👉️ ['design', 'test']

The list.copy method returns a shallow copy of the object on which the method was called.

You can also use a default argument for the list in the class's __init__() method.

main.py
class Employee(): # 👇️ default argument of None def __init__(self, name, tasks=None): self.name = name if tasks is None: tasks = [] self.tasks = tasks def add_tasks(self, task): self.tasks.append(task) return self.tasks alice = Employee('Alice') bob = Employee('Bobbyhadz') print(bob.tasks) # 👉️ [] bob.add_tasks('develop') print(bob.tasks) # 👉️ ['develop'] print(alice.tasks) # 👉️ []

We used a default value of None for the tasks variable.

If no argument is passed, we initialize tasks to an empty list.

This is useful because using a default argument of an empty list for the tasks variable would introduce the problem where multiple instances refer to the same list.

main.py
class Employee(): # ⛔️ BAD - using empty list default argument def __init__(self, name, tasks=[]): self.name = name self.tasks = tasks def add_tasks(self, task): self.tasks.append(task) return self.tasks alice = Employee('Alice') bob = Employee('Bobbyhadz') bob.add_tasks('develop') print(bob.tasks) # 👉️ ['develop'] print(alice.tasks) # 👉️ ['develop']

Notice that adding a value to the list in one instance also added the value to the tasks list in the other instance.

This is because the tasks instance variable points to the same list (same location in memory) for both instances.

You can get around this by using a None default value like we did in the previous example.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.