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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:34:00+00:00 2026-05-26T12:34:00+00:00

Having issues with when objects are changed and when they aren’t in Python. Here

  • 0

Having issues with when objects are changed and when they aren’t in Python. Here is my poorly contrived example below:

class person:
    age = 21

class bar:
    def __init__(self, arg1):
        self.foo = arg1
        self.foo.age = 23

def baz(arg1):
    arg1.age = 27

def teh(arg1):
    arg1 = [3,2,1]

Person1 = person()
bar1 = bar(Person1)

print Person1.age
print bar1.foo.age

baz(Person1)

print Person1.age
print bar1.foo.age

meh = [1,2,3]
teh(meh)
print meh

The output is

23
23
27
27
[1, 2, 3]

So when we declare Person1, Person1.age is 21. A reference to this object is passed to the class constructor of another class instance of bar, called bar1. Any changes made to this reference will change Person1.

This is also the case when we pass Person1 to a normal function, the Person1.age now equals 27.

But why doesn’t this work on the variable “meh”? Certainly, if we assign a variable a = meh and change a = [6, 6, 6], then meh will also be changed. I’m confused. Is there any literature on how all this works?

  • 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-26T12:34:00+00:00Added an answer on May 26, 2026 at 12:34 pm

    I can see three fundamental Python concepts that can shine some light on the question:

    1) First, an assignment from a mutable object like in

    self.foo = arg1
    

    is like copying a pointer (and not the value pointed to): self.foo and arg1 are the same object. That’s why the line that follows,

    self.foo.age = 23
    

    modifies arg1 (i.e. Person1). Variables are thus different “names” or “labels” that can point to a unique object (here, a person object). This explains why baz(Person1) modifies Person1.age and bar1.foo.age to 27, since Person1 and bar1.foo are just two names for the same person object (Person1 is bar1.foo returns True, in Python).

    2) The second important notion is that of assignments. In

    def teh(arg1):
        arg1 = [3,2,1]
    

    variable arg1 is local, so that the code

    meh = [1,2,3]
    teh(meh)
    

    first does arg1 = meh, which means that arg1 is an additional (local) name for list meh; but doing arg1 = [3, 2, 1] is like saying “I changed my mind: arg1 will from now on be the name of a new list, [3, 2, 1]”. The important thing to keep in mind here is that assignments, despite being denoted with an “equal” sign, are asymmetrical: they give to a (mutable) object on the right-and-side an additional name, given in the left-hand side (that’s why you can’t do [3, 2, 1] = arg1, as the left-hand side must be a name [or names]). So, arg1 = meh; arg1 = [3, 2, 1] cannot change meh.

    3) The last point is related to the question title: “passing by value” and “passing by reference” are not concepts that are relevant in Python. The relevant concepts are instead “mutable object” and “immutable object“. Lists are mutable, while numbers are not, which explains what you observe. Also, your Person1 and bar1 objects are mutable (that’s why you can change the person’s age). You can find more information about these notions in a text tutorial and a video tutorial. Wikipedia also has some (more technical) information. An example illustrates the difference of behavior between mutable and immutable:

    x = (4, 2)
    y = x  # This is equivalent to copying the value (4, 2), because tuples are immutable
    y += (1, 2, 3)  # This does not change x, because tuples are immutable; this does y = (4, 2) + (1, 2, 3)
    
    x = [4, 2]
    y = x  # This is equivalent to copying a pointer to the [4, 2] list
    y += [1, 2, 3]  # This also changes x, because x and y are different names for the same (mutable) object
    

    The last line is not equivalent to y = y + [1, 2, 3] because this would only put a new list object in variable y instead of changing the list referred to by both y and x.

    The three concepts above (variables as names [for mutable objects], asymmetrical assignment, and mutability/immutability) explain many of Python’s behaviors.

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

Sidebar

Related Questions

I'm having a lot of issues with NSDate objects being prematurely deallocated. I suspect
I'm having issues with TIFFs Here is what I have to do, we have
Not sure why I keep having issues with this. Anyway, here are the models:
Update: Using Django 1.2.1 and Python 2.5.2 as offered by Dreamhost. I'm having issues
Having issues referencing $(this) from within a the nested ajax 'success' function... I know
Still having issues with this problem. Please help if you can. So I am
Im having issues getting this to work, maybe its not even possible? I have
Im having issues vertically positioning text inside of a text input field in Firefox.
I'm having issues with my SQL Reporting Services reports. I'm using a custom font
I'm having issues creating an ActionLink using Preview 5. All the docs I can

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.