Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6556171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:54:38+00:00 2026-05-25T12:54:38+00:00

A few times I accidentally modified the input to a function. Since Python has

  • 0

A few times I accidentally modified the input to a function. Since Python has no constant references, I’m wondering what coding techniques might help me avoid making this mistake too often?

Example:

class Table:
  def __init__(self, fields, raw_data):
    # fields is a dictionary with field names as keys, and their types as value 
    # sometimes, we want to delete some of the elements 
    for field_name, data_type in fields.items():
      if some_condition(field_name, raw_data):
        del fields[field_name]
    # ...


# in another module

# fields is already initialized here to some dictionary
table1 = Table(fields, raw_data1) # fields is corrupted by Table's __init__
table2 = Table(fields, raw_data2)

Of course the fix is to make a copy of the parameter before I change it:

  def __init__(self, fields, raw_data):
    fields = copy.copy(fields)
    # but copy.copy is safer and more generally applicable than .copy 
    # ...

But it’s so easy to forget.

I’m half thinking to make a copy of each argument at the beginning of every function, unless the argument potentially refers to a large data set which may be expensive to copy or unless the argument is intended to be modified. That would nearly eliminate the problem, but it would result in a significant amount of useless code at the start of each function. In addition, it would essentially override Python’s approach of passing parameters by reference, which presumably was done for a reason.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:54:38+00:00Added an answer on May 25, 2026 at 12:54 pm

    First general rule: don’t modify containers: create new ones.

    So don’t modify your incoming dictionary, create a new dictionary with a subset of the keys.

    self.fields = dict( key, value for key, value in fields.items()
                         if accept_key(key, data) )
    

    Such methods are typically slightly more efficient then going through and deleting the bad elements anyways. More generally, its often easier to avoid modifying objects and instead create new ones.

    Second general rule: don’t modify containers after passing them off.

    You can’t generally assume that containers to which you have passed data have made their own copies. As result, don’t try to modify the containers you’ve given them. Any modifications should be done before handing off the data. Once you’ve passed the container to somebody else you are no longer the sole master of it.

    Third general rule: don’t modify containers you didn’t create.

    If you get passed some sort of container, you do not know who else might be using the container. So don’t modify it. Either use the unmodified version or invoke rule1, creating a new container with the desired changes.

    Fourth general rule: (stolen from Ethan Furman)

    Some functions are supposed to modify the list. That is their job. If this is the case make that apparent in the function name (such as the list methods append and extend).

    Putting it all together:

    A piece of code should only modify a container when it is the only piece of code with access to that container.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen the following a few times lately: function foo(array $arg = NULL) {
A situation has come up a few times in the last few weeks when
This has happened quite a few times. Application working fine. Then I chose a
I know this has been asked a few times here. But none of the
I want to create an object in python that has a few attributes and
This has been asked few times but I think it's still worth checking with
I know this has been asked a few times, but I can't find a
I know this has been asked a few times but please bear with me.
A few times already, I got into situations where one of my SVN repository
I have seen a few times people using -1 as opposed to 0 when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.