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

  • Home
  • SEARCH
  • 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 7866811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:30:59+00:00 2026-06-03T00:30:59+00:00

I am new to python and have been unable to find anything that explains

  • 0

I am new to python and have been unable to find anything that explains the behavior I am seeing below. I noticed the problem when returning a list from a method and distilled it down to the simplest form that shows the problem. I figured out a workaround but would like to know what I am missing in my understanding since I would expect both examples to behave the same.

class MyCount:
    """A simple count test to show return list problem"""
    def __init__(self):
        self.next = [0]

    def count_good(self):
        self.next[0] += 1
        return [self.next[0]]

    def count_bad(self):
        self.next[0] += 1
        return self.next # returning using this form corrupts the recieving list


c=MyCount()
result=4*[0]
result[0]=c.count_good()
result[1]=c.count_good()
result[2]=c.count_bad()
print result
result[3]=c.count_bad()
print result


>>> c=MyCount()
>>> result=4*[0]
>>> result[0]=c.count_good()
>>> result[1]=c.count_good()
>>> result[2]=c.count_bad()
>>> print result
[[1], [2], [3], 0]
>>> result[3]=c.count_bad()
>>> print result
[[1], [2], [4], [4]]   <--- the return changed the previous item in the list
>>>
>>> c=MyCount()
>>> result=4*[0]
>>> c.count_good()
[1]
>>> c.count_good()
[2]
>>> c.count_bad()
[3]
>>> c.count_bad()  <--- seems to work fine when not returning to a list
[4]
>>> 
  • 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-03T00:31:01+00:00Added an answer on June 3, 2026 at 12:31 am

    When you return self.next, you’re returning a reference to the actual list object which self.next refers to, not a copy. Thus, any changes made to that original list object from anywhere will be reflected in all of the places which reference that original object.

    In order to return a copy, you should either make a full slice:

    return self.next[:]
    

    or use the list() function:

    return list(self.next)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to python and I have a serious problem that I cannot
hello im new to python and have been reading over the documentation and am
I am new to Python and I have been cleaning up a messy database
I am really new to Python and I have been looking for an example
I'm new to Python and I have been trying to search through html with
I am new to Django & Python and have been following a tutorial, but
I'm new to python and have hit a problem with an SQL query I'm
I'm new to Python. I have a method that begins: def foo(self, list): length
I'm new to python and have been trying to figure this out all day.
I'm new to Python (I have been programming in Java for multiple years now

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.