Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up

What is the difference between shallow copy and deep copy, and what is the mutable default argument trap?

Python assigns by reference, so b = a gives two names for one object.

  • Shallow copycopy.copy(x), list(x), or x[:]. A new outer container holding the same inner objects. Mutating a nested list still affects both.
  • Deep copycopy.deepcopy(x). Recursively copies everything, and handles circular references.

The mutable default argument trap is the classic Python gotcha:

def add(item, bucket=[]):     # WRONG
    bucket.append(item)
    return bucket

add(1)   # [1]
add(2)   # [1, 2]  <- the same list!

The default is evaluated once, when the function is defined, not on each call. So every call without an explicit argument shares one list.

The fix:

def add(item, bucket=None):
    if bucket is None:
        bucket = []
    bucket.append(item)
    return bucket

Note: The same applies to dictionaries, sets, and any object constructed in the signature — including datetime.now(), which freezes at import time.

All Python interview questions

Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up as