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 830937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:07:29+00:00 2026-05-15T04:07:29+00:00

A simple question, perhaps, but I can’t quite phrase my Google query to find

  • 0

A simple question, perhaps, but I can’t quite phrase my Google query to find the answer here. I’ve had the habit of making copies of objects when I pass them into object constructors, like so:

...
def __init__(self, name):
    self._name = name[:]
...

However, when I ran the following test code, it appears to not be necessary, that Python is making deep copies of the object values upon object instantiation:

>>> class Candy(object):
...     def __init__(self, flavor):
...             self.flavor = flavor
...
>>> flav = "cherry"
>>> a = Candy(flav)
>>> a
<__main__.Candy object at 0x00CA4670>
>>> a.flavor
'cherry'
>>> flav += ' and grape'
>>> flav
'cherry and grape'
>>> a.flavor
'cherry'

So, what’s the real story here? Thanks!

EDIT:

Thanks to @Olivier for his great answer. The following code documents a better example that Python does copy by reference:

>>> flav = ['a','b']
>>> a = Candy(flav)
>>> a.flavor
['a', 'b']
>>> flav[1] = 'c'
>>> flav
['a', 'c']
>>> a.flavor
['a', 'c']
  • 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-15T04:07:29+00:00Added an answer on May 15, 2026 at 4:07 am

    It is because strings are immutable.

    The operator +=, rather confusingly, actually reassigns the variable it is applied to, if the object is immutable:

    s = 'a'
    ids = id(s)
    s += 'b'
    ids == id(s) # False, because s was reassigned to a new object
    

    So, in your case, in the beginning, both flav and a.flavor point to the same string object:

    flav --------\
                   'cherry'
    a.flavor ----/
    

    But when you write flav += 'and grape' the variable flav gets reassigned to a new string object:

    flav --------> 'cherry and grape'
    a.flavor ----> 'cherry' # <-- that string object never changes
    

    It is confusing, because usually, when you call an operator on a variable, it doesn’t change the variable. But just in the case of an immutable object, it does reassign the variable.

    So the final answer to your question is, yes, it makes sense to copy the objects upon instantiation, especially if you are expecting a mutable object (which is often the case). It the object was immutable, it will not harm to copy it anyway.

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

Sidebar

Related Questions

Simple question, but could somehow not find any answer on google (maybe it's just
Perhaps a simple to be answered question, but I did not find a satisfying
simple problem, but perhaps no simple solution, at least I can't think of one
I have a pretty simple question which perhaps someone familiar with Server/Client design &
Simple question, but one that I've been curious about...is there a functional difference between
Simple question: Can a swing frame be completely modal ( block all others windows
This is a very simple, probably too simple question but I'm afraid my limited
this may seem like a very simple question, but I want to dump some
I have a feeling that this is a subjective question, but perhaps there's something
here's a very simple js but i don't know where to begin. in a

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.