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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:02:56+00:00 2026-06-03T09:02:56+00:00

Sorry if the question isn’t that clear in the title but I had a

  • 0

Sorry if the question isn’t that clear in the title but I had a limited amount of space,
To clarify…

In my code I have a class called empDB that creates an empty list for storing objects.

Too add new objects to the list I created a method in empDB called appendEmp. appendEmp takes one parameter that specifies the class which it will instantiate and then add it to the list. If no parameter is provided it defaults to the class Employee().

here is my problem

when I call the method appendEmp it creates new objects in the list, however when I try to instantiate an object thats already in the list it will only create an alias of it. I need it to create a new one each time so that i can edit the attributes of each object in the list individually.

here is a snippet of my code:

class empDB:

    def __init__(self, lst= []):                  #creates empty list
        self.lst = lst

    def appendEmp(self, emp=Employee()):
        self.lst=self.lst+[emp]

basicly this is what happens now

>>> db=empDB()
>>> db.appendEmp()
>>> db.appendEmp()
>>> db[0] is db[1]
True

I need it to be false

  • 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-03T09:02:57+00:00Added an answer on June 3, 2026 at 9:02 am

    You can solve your problem like that:

    class empDB:
        def __init__(self, lst= []):  #creates empty list
            self.lst = lst
        def appendEmp(self, emp=None):
            if emp is None:
                empt = Employee()
            self.lst=self.lst+[emp]
    

    The issue was caused by mutable value being assigned as the default argument. I replaced it with None, although if you would like to accept this value as the argument of appendEmp(), you can replace it with something else.

    More on dangers of using mutable default arguments you can read here: Hidden features of Python: Dangers of mutable default arguments

    Ps. You may also wish to do the same for __init__() method.

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

Sidebar

Related Questions

My question title isn't clear, sorry. I tried ;) I have a binary string
[I'm sorry that this isn't directly a programming question. But I have recently switched
Sorry if this question isn't clear. The short story is that we had someone
Sorry if this is a silly question :-) Background I have legacy code that
I'm sorry if my question isn't clear. I have a class contain some properties
The title to this question isn't written very well. Sorry 'bout that. I'm curious
Quick question (and sorry if it's already been asked, I have looked but couldn't
Sorry if the question isn't phrased very well but I'm new to SSAS and
Sorry if this isn't the appropriate place to put this question, but I'm new
Sorry for the bad headline, but my english isn't that good. Assume the following

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.