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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:55:32+00:00 2026-05-17T15:55:32+00:00

class Foo(object): def __init__(self,x): self.x = x self.is_bar = False def __repr__(self): return str(self.x)

  • 0
class Foo(object):
    def __init__(self,x):
        self.x = x
        self.is_bar = False
    def __repr__(self): return str(self.x)

class Bar(object):
    def __init__(self,l = []):
        self.l = l
    def add(self,o):
        self.l += [o]
    def __repr__(self): return str(self.l)

def foo_plus_foo(f1,f2):
    t = Bar()
    if not (f1.is_bar and f2.is_bar):
        f1.is_bar = True
        f2.is_bar = True
        t.add(f1)
        t.add(f2)
        print 'HERE'
    return t

if __name__ == '__main__':
    li = [Foo(1), Foo(2)]
    print foo_plus_foo(li[0],li[1])
    print foo_plus_foo(li[0],li[1])

UNEXPECTED OUTPUT:

HERE
[1, 2]
[1, 2]

EXPECTED OUTPUT:

HERE
[1, 2]
[]

What is happening? What did I do wrong? Why is python using old value? What do I do to avoid this?

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-17T15:55:33+00:00Added an answer on May 17, 2026 at 3:55 pm

    Never. Do. This.

    def __init__(self,l = []):
    

    Never.

    One list object is reused. And it’s Mutable, so that each time it’s reused, the one and only [] created in your method definition is updated.

    Always. Do. This.

    def __init__( self, l= None ):
        if l is None: l = []
    

    That creates a fresh, new, unique list instance.

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

Sidebar

Related Questions

If I have this: class foo(object): @property def bar(self): return 0 f = foo()
How to make a Python class serializable? class FileItem: def __init__(self, fname): self.fname =
abstract class Foo { private List<Object> container; private bool update; Foo Foo() { container
I am using a class Foo that provides these methods: String overloadedMethod(Object) String overloadedMethod(Goo)
Is there any meaningful distinction between: class A(object): foo = 5 # some default
class Foo { static bool Bar(Stream^ stream); }; class FooWrapper { bool Bar(LPCWSTR szUnicodeString)
As an example: public class Foo { private Foo() {} } public class Bar
Instead of writing code like this every time I define a class: class Foo(object):
class Foo(models.Model): title = models.CharField(max_length=20) slug = models.SlugField() Is there a built-in way to
Given this class class Foo { // Want to find _bar with reflection [SomeAttribute]

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.