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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:56:17+00:00 2026-05-31T16:56:17+00:00

I have defined a class Listener and created a dictionary of Listener objects. Each

  • 0

I have defined a class Listener and created a dictionary of Listener objects. Each listener has an id to identify them, and a list of artists they listen to, artists = []. Adding something to the artists list adds it for all instances of the Listener class, rather than the referred instance. This is my problem.

The Listener class is defined as follows:

class Listener:
    id = ""
    artists = []

    def __init__(self, id):
        self.id = id

    def addArtist(self, artist, plays):
        print self.id # debugging...
        print "pre: ", self.artists
        self.artists.append(artist)
        print "post: ", self.artists

Here is my debugging test code:

def debug():
    listeners = {}
    listeners["0"] = Listener("0")
    listeners["1"] = Listener("1")

    listeners["0"].addArtist("The Beatles", 10)
    listeners["0"].addArtist("Lady Gaga", 4)
    listeners["1"].addArtist("Ace of Base", 5)

And the output:

0
pre:  []
post:  ['The Beatles']
0
pre:  ['The Beatles']
post:  ['The Beatles', 'Lady Gaga']
1
pre:  ['The Beatles', 'Lady Gaga']
post:  ['The Beatles', 'Lady Gaga', 'Ace of Base']

My expected output is that the final addArtist("Ace of Base", 5) call would result in the output

1
pre:  []
post:  ['Ace of Base']

Is this a subtlety of Python I’m not understanding? Why is this the output and how can I get the desired output instead? Thanks!

  • 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-31T16:56:18+00:00Added an answer on May 31, 2026 at 4:56 pm

    You don’t want the members declared inside the class, but just set in the __init__ method:

    class Listener:
        def __init__(self, id):
            self.id = id
            self.artists = []
    
        def addArtist(self, artist, plays):
            print self.id # debugging...
            print "pre: ", self.artists
            self.artists.append(artist)
            print "post: ", self.artists
    

    If you have a class like

    class A:
      x=5
    

    Then x is a member of the class and not a member of instances of that class. This can be confusing, since python lets you access class members through the instance:

    >>> a=A()
    >>> print a.x
    5
    

    But you can also access it through the class itself:

    >>> print A.x
    5
    

    It would even appear that this works properly:

    >>> a1=A()
    >>> a2=A()
    >>> a1.x=6
    >>> print a1.x
    6
    >>> print a2.x
    5
    

    but what has actually happened is that you’ve put a new x into the a1 instance, which will be printed instead of the class member, which still has its original value:

    >>> print A.x
    5
    

    You only start to see a difference when you have something that can be changed, like a list:

    class A:
      l=[]
    
    >>> a1=A()
    >>> print a1.l
    []
    >>> a2=A()
    >>> print a2.l
    []
    >>> a1.l.append(5)
    >>> print a1.l
    [5]
    >>> print a2.l
    [5]
    >>> print A.l
    [5]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a listener interface defined as: class Listener { public: virtual
I have defined a class A and derived a new class B from A
I have defined an Event class: Event and all the following classes inherit from
Let's say I have defined the following class: public abstract class Event { public
I have a class defined as public class viewGroups extends ListActivity Somewhere in the
I have a class defined as: class Obj { public: int width, height; Obj(int
I have a class defined like this, with the appropriate getter and setter methods...
I have made a Java class where I have defined a constructor and some
I have a template class defined like so: template <class T> class Command {
I have a Customer class defined like this: @Entity public class Customer { //...

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.