Skip to content

Issues with Address object #26

Open
@philhy

Description

When trying to create multiple routes using a loop, I encountered the issue of the addresses from the previous iteration still being in the addresses variable. In address.py I believe I found the issue. Below is the current version of the code.

image

The issue here is that the addresses parameter is assigned a kwarg of an empty list. However, in python using a mutable object such as a list as a kwarg means that whenever the function is called the changes made to that parameter are stored in the function itself, thereby effecting the output every time the function is called. In this case, every time you create an Address object the addresses are stored in the addresses list and are not removed when you create another Address object. This can be avoided by either re-running the .py file if you're in an IDE or restarting the kernel if you're in Jupyter Notebook. However, making a simple adjustment in the source code will allow you to run the function multiple times and have the list of addresses update every time the function is called.

image

Here, instead of assigning a list to the address parameter, we assign it a None value and create the empty list variable inside the function itself. This way every time an Address object is created the addresses list will clear itself out before adding the new addresses.

Hope anyone who had the same trouble finds this method useful, and feel free to correct any mistakes in my explanation or provide an even simpler solution.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions