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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:59:16+00:00 2026-06-16T01:59:16+00:00

These two class definitions are structured identically, except in the weird() class definition the

  • 0

These two class definitions are structured identically, except in the weird() class definition the attribute ‘d’ is a list, and in the normal() class definition its an int. I dont understand why the weird() class results in self.d = d, while this is not the case with the class normal(). Why does Python treat int and list differently in this situation?

class weird:
    def __init__(self):
        d = [1,2,3]
        self.d = d
        for x in range(3):
            d[x] = 10+x 
        print "d =", d
        print "self.d =", self.d

class normal:
    def __init__(self):
        d = 1
        self.d = d
        d = 11
        print "d =", d
        print "self.d =", self.d

And when I run the code, I get

>>> a=weird()
d = [10, 11, 12]
self.d = [10, 11, 12]
>>> b=normal()
d = 11
self.d = 1
  • 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-06-16T01:59:17+00:00Added an answer on June 16, 2026 at 1:59 am

    You are confusing assignment with mutation of an object.

    In one method, you assigned new values to d, changing what it points to:

    d = 11
    

    but in the other method you changed values contained in d:

    d[x] = 10 + x
    

    Note the [x] there; you are, in essence, telling python to do this:

    d.__setitem__(x, 10 + x)
    

    The value of the variable d, which points to a list, is never changed. The values in the list referred to by d, do change.

    Take a look at this previous answer of mine, which goes into more detail as to what the difference is between these two statements: Python list doesn't reflect variable change.

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

Sidebar

Related Questions

I have a code base, in which for Matrix class, these two definitions are
i have these two models public class Group { [Key] public int GroupID {
Let's say we have these two classes: public class Base { public static int
What is the difference between these two class declarations? I don't understand why @class
I have these two classes public class Person { } public class Company {
I am not clear on the actual difference between these two styles of class
This is easier to explain with an example. Given these two classes: public class
I have these two methods on a class that differ only in one method
I have these two simple files that define a C++ class with a tryME
I use these two files here and here . I created a class in

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.