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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:43:07+00:00 2026-05-21T12:43:07+00:00

Hi I have created a List of Objects. Each object contains a Set. I

  • 0

Hi
I have created a List of Objects. Each object contains a Set. I want to update the set’s contents for all the objects in the list. The code that i wrote to accomplish this is

class Player:
    name = ""
    cardsInHand = set()
    hasBid = False
    def __init__(self, name):
        self.name = name 

class CardDeck:
    deck = []

    def __init__(self):
        for i in range(39) :
        if i%10>0 and i%10<9 :
                self.deck.append(i)

    def dealCards(self,player):
        cardIndex = 0
        for tempPlayer in player:
            for j in range(4): # since want to add four elements at a time
                tempPlayer.cardsInHand.add(self.deck.pop(cardIndex))
                cardIndex = cardIndex +1

in the main method I am calling the above classes with the following code

players = []
players.append(Player("Player0"))
players.append(Player("Player1"))
players.append(Player("Player2"))
players.append(Player("Player3"))

cards.dealCards(players)

The problem is that dealCards method adds the elements to all the sets of objects. Instead of 4 elements in each object’s set, I endup with same 16 elements in each objects’s set?
I am new to python, am i doing something wrong ?

  • 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-21T12:43:08+00:00Added an answer on May 21, 2026 at 12:43 pm

    You’re creating class attributes.

    class Player:
        def __init__(self, name):
            self.name = name 
            self.cardsInHand = set()
            self.hasBid = False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of objects. Each object contains a property called 'DisplayName'. I
i have an array list of custom objects. each object contains a value i
I have an assembly which contains several UserControl objects that I want to be
I have created a list view that displays the names and dates of items
We have created a new List View Style that shows thumbnails from a picture
I have created a nested list with drag/drop functionality. My issue is that I
I have created a dynamic list picker script using Jquery 1.3 and PHP that
What I have in my project is the binary file that contains list of
I have a list of custom objects that I am loading into an ActivityList
I have a repeater that is bound to a List(of T) object collection. It's

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.