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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:51:08+00:00 2026-05-26T23:51:08+00:00

Here is a python class: class TxdTest(object): def __init__(self, name = ”, atrributes =

  • 0

Here is a python class:

class TxdTest(object):
    def __init__(self, name = '', atrributes = []):
        self.name = name
        self.attributes = atrributes

and then I use it like this:

def main():
    for i in range(3):
        test = TxdTest()
        print test.name
        print test.attributes
        test.name = 'a'
        test.attributes.append(1)

so, what’s the result? The result is:

[]

[1]

[1, 1]

Why the ‘self.attributes’ in class still obtain the value ?

  • 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-26T23:51:09+00:00Added an answer on May 26, 2026 at 11:51 pm

    When passing a mutable object (like a list) to a function or method in python, a single object is used across all invocations of the function or method. This means that ever time that function or method (in this case, your __init__ method) is called, the exact same list is used every time, holding on to modifications made previously.

    If you want to have an empty list as a default, you should do the following:

    class TxdTest(object):
        def __init__(self, name = '', atrributes = None):
            self.name = name
            if attributes is None
                 self.attributes = []
            else:
                 self.attributes = attributes
    

    For a detailed explanation of how this works see: “Least Astonishment” in Python: The Mutable Default Argument, as mentioned by bgporter.

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

Sidebar

Related Questions

I have the following situation in my python code: class Parent(object): def run(self): print
Suppose that I have the following python base class: class BaseClass(object): def a(): This
I'm trying to use an Objective-C class made in Python to do this but
Python noob here, Currently I'm working with SQLAlchemy, and I have this: from __init__
I use this code to define my class in GAE Python: class Pair(db.Model): find
I want to override my Python class's __getattribute__ and __setattr__ methods. My use case
I looked through the tutorials for the python logging class here and didnt see
I need to create a 'container' object or class in Python, which keeps a
Python novice here. I am using python2.7.2 on Windows7. I have installed the PyWin32
I have looked at the documentation here: http://docs.python.org/dev/library/xml.etree.elementtree.html#xml.etree.ElementTree.SubElement The parent and tag argument seems

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.