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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:31:19+00:00 2026-06-01T21:31:19+00:00

Here is a code: >>> class A(object): … value = [] … def method(self,

  • 0

Here is a code:

>>> class A(object):
...     value = []
...     def method(self, new_value):
...         self.value.append(new_value)
... 
>>> a = A()
>>> a.value
[]
>>> a.method(1)
>>> b = A()
>>> b.value
[1]
>>> b.method(2)
>>> b.value
[1, 2]
>>> a.value
[1, 2]

This happens only with lists. Is the only way to deffine value in __init__?
How to normally define default class values in python?

UPD

thank you for your responses

>>> class B(object):
...     value = "str"
...     def method(self):
...         self.value += "1"
... 
>>> a = B()
>>> a.value
'str'
>>> a.method()
>>> a.value
'str1'
>>> b = B()
>>> b.value
'str'

I don’t get, why list is shared but str is not?

  • 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-01T21:31:20+00:00Added an answer on June 1, 2026 at 9:31 pm

    The value you are defining is not an instance field for your class, its more like a static field. But python doesn’t mind if you access this field from instances. So, even if you access this field from instances, it is not a different list for each instance. Basically, you are appending to the same list every time the method is called.

    You’ll have to do this

    class A(object):
    
        def __init__(self):
            self.value = []
    
        def method(self, new_value):
            self.value.append(new_value)
    

    Now you have a different list created for each instance.

    EDIT: Let me try to explain what happens when you use a str.

    class A(object):
    
        self.value = 'str'
    
        def method(self):
            self.value += '1'
    

    That last line in the previous code is the same as this:

            self.value = self.value + '1'
    

    Now, this makes it abit easier to see what’s going on. First, python gets the
    value from self.value. Since there is no instance field defined yet on self,
    this will give 'str'. Add '1' to that and sets it to the instance field
    called value. This is like

    self.value = 'str1'
    

    which is the same as you’d set an instance field in the __init__ method (in my
    first snippet of code).

    self.value = []
    

    Does that make it clear?

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

Sidebar

Related Questions

Here is my code: class a(object): d='ddd' def __contains__(self): if self.d:return True b=a() print
Here's my code: class Pop(object): def holder(self): self.boobs = 16 self.sent = pop def
Here's the faulty bit of my code: class Room(object): def __init__(self): self.hotspots = []
Here is the code: class Dummy(object): def __init__(self, v): self.ticker = v def main():
I have the following situation in my python code: class Parent(object): def run(self): print
I have the following code: class Hello(object): def __init__(self, names): self.names = names def
In Python, consider I have the following code: class SuperClass(object): def __init__(self, x): self.x
Here is some code: class Person def initialize(age) @age = age end def age
I found this code here class Usable; class Usable_lock { friend class Usable; private:
In the following code: public class A { public A():this(null){} public A(string b){/*code here*/}

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.