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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:51:49+00:00 2026-05-28T04:51:49+00:00

I was working on a simple script today when I noticed a strange quirk

  • 0

I was working on a simple script today when I noticed a strange quirk in the way Python treats instance variables.

Say we have a simple object:

class Spam(object):
    eggs = {}

    def __init__(self, bacon_type):
        self.eggs["bacon"] = bacon_type

    def __str__(self):
       return "My favorite type of bacon is " + self.eggs["bacon"]

And we create two instances of this object with separate arguments:

spam1 = Spam("Canadian bacon")
spam2 = Spam("American bacon")

print spam1
print spam2

The results are puzzling:

My favorite type of bacon is American bacon
My favorite type of bacon is American bacon

It seems like the “eggs” dictionary is shared between all the different “Spam” instances — either that or it is overwritten every time a new instance is created. This isn’t really a problem in every day life, since we can solve it by declaring the instance variable in the initialization function:

class Spam(object):

    def __init__(self, bacon_type):
        self.eggs = {}
        self.eggs["bacon"] = bacon_type

    def __str__(self):
        return "My favorite type of bacon is " + self.eggs["bacon"]

spam1 = Spam("Canadian bacon")
spam2 = Spam("American bacon")

print spam1
print spam2

With the code written this way, the result is what we expect:

My favorite type of bacon is Canadian bacon
My favorite type of bacon is American bacon

So while I’m not held up by this behavior, I don’t understand why Python works this way. Can anyone shed some light on this?

  • 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-28T04:51:49+00:00Added an answer on May 28, 2026 at 4:51 am

    As Ignacio has posted, variables which are assigned to at class scope in Python are class variables. Basically, in Python, a class is just a list of statements under a class statement. Once that list of statements finishes executing, Python scoops up any variables that were created during the course of that execution and makes a class out of them. If you want an instance variable, you actually do have to assign it to the instance.

    On another note: it sounds like you may be coming to this from a Java (or Java-like) perspective. So perhaps you know that because Java requires variables to be explicitly declared, it needs to have instance variable declarations at class scope.

    class Foo {
        String bar;
        public Foo() {
            this.bar = "xyz";
        }
    }
    

    Note that only the declaration is at class scope. In other words, the memory allocated for that variable is part of the class “template,” but the actual value of the variable is not.

    Python doesn’t have any need for variable declarations. So in the Python translation, you just drop the declaration.

    class Foo:
        # String bar;  <-- useless declaration is useless
        def __init__(self):
            self.bar = "xyz"
    

    The memory will be allocated when it’s needed; only the assignment is actually written out. And that goes in the constructor, just like in Java.

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

Sidebar

Related Questions

I'm working on a simple GUI Python script to do some simple tasks on
I'm working on a simple Python script that can use subprocess and/or os to
I have a simple working PHP script to write an HTML table from a
I am working on a project that I have to Write a simple script
I'm working with a simple script to move an img object between two divs
I'm currently working on a simple Python script that connects to an IP/Port and
Working on a simple poker script in PHP and need a way to determine
I have a simple script updating and showing a useraccount. (working with the management
I was working on a simple GUI script yesterday using jdbc in eclipse. When
I'm trying to get this simple PowerShell script working, but I think something is

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.