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

The Archive Base Latest Questions

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

I read this about Python classes ( link ) and it seems to be

  • 0

I read this about Python classes (link) and it seems to be the issue I am having.

Here is an excerpt from my class and other code:

class s_board:

    def __init__(self):
        self.__board = [[n for n in range(1, 10)] for m in range(81)]
        self.__solved = [False for m in range(81)]

    def copy(self):
        b = s_board()
        b.__board = self.__board[:]
        b.__solved = self.__solved[:]
        return b

if __name__ == '__main__':
    A = s_board()
    B = A.copy()
    B.do_some_operation_on_lists()

When I call B’s method that does something to the list, A’s lists seem to be affected as well.

So my questions:

  • Am I not copying the class or the lists correctly?
  • Is there another issue here?
  • How do I fix it so that I get a new copy of the class?
  • 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-26T20:31:09+00:00Added an answer on May 26, 2026 at 8:31 pm

    self.__board[:] creates a new list containing references to all the same objects that were in self.__board. Since self.__board contains lists, and lists are mutable, you end up with the two s_board instances with partially aliased data, and changing one affects the other.

    As Raymond Hettinger suggested, you can use the copy.deepcopy to (mostly) guarantee that you take a true copy of an object and don’t share any data. I say mostly, as I believe there are some strange objects that deepcopy will not work on, but for normal things like lists and straightforward classes it will work fine.

    I have an additional suggestion though. You call b = s_board(), which goes to all the effort of constructing the lists for the new blank board, and then you throw them away by assigning to b.__board and b.__solved. It seems to be like it would be better to do something like the following:

    class s_board:
    
        def __init__(self, board=None, solved=None):
            if board is None:
                self.__board = [[n for n in range(1, 10)] for m in range(81)]
            else:
                self.__board = copy.deepcopy(board)
            if solved is None:
                self.__solved = [False for m in range(81)]
            else:
                self.__solved = copy.deepcopy(solved)
    
        def copy(self):
            b = s_board(self.__board, self.__solved)
            return b
    

    Now if you call A = s_board() you get a new blank board, and if you call A.copy() you get a distinct copy of A, without having had to allocate and then discard a new blank board.

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

Sidebar

Related Questions

I have read this article from High Scalability about Stack Overflow and other large
I've read in Expert Python Programming about this edge case. Check this code: def
I am sure I read about this the other day but I can't seem
I have read this post about how to test private methods. I usually do
I just read this post about why new-line warnings exist, but to be honest
I have read this article about 400% boost of your website . This is
I've read this book section about git branches. I have create a branch called
I just started to read about this new technology... Does someone have some knowledge
Today I read this blog entry by Roger Alsing about how to paint a
You can read this question where I ask about the best architecture for a

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.