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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:06:03+00:00 2026-06-15T20:06:03+00:00

Possible Duplicate: Python: Difference between class and instance attributes I’m trying to get my

  • 0

Possible Duplicate:
Python: Difference between class and instance attributes

I’m trying to get my head around OOP in Python and I’m a bit confused when it comes to declare variables within a class. Should I declare them inside of the __init__ procedure or outside it? What’s the difference?

The following code works just fine:

# Declaring variables within __init__
class MyClass:
    def __init__(self):
        country = ""
        city = ""
    def information(self):
        print "Hi! I'm from %s, (%s)"%(self.city,self.country)

me = MyClass()
me.country = "Spain"
me.city = "Barcelona"
me.information()

But declaring the variables outside of the __init__ procedure also works:

# Declaring variables outside of __init__
class MyClass:
    country = ""
    city = ""
    def information(self):
        print "Hi! I'm from %s, (%s)"%(self.city,self.country)

me = MyClass()
me.country = "Spain"
me.city = "Barcelona"
me.information()
  • 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-15T20:06:04+00:00Added an answer on June 15, 2026 at 8:06 pm

    In your first example you are defining instance attributes. In the second, class attributes.

    Class attributes are shared between all instances of that class, where as instance attributes are “owned” by that particular instance.

    Difference by example

    To understand the differences let’s use an example.

    We’ll define a class with instance attributes:

    class MyClassOne:
        def __init__(self):
            self.country = "Spain"
            self.city = "Barcelona"
            self.things = []
    

    And one with class attributes:

    class MyClassTwo:
        country = "Spain"
        city = "Barcelona"
        things = []
    

    And a function that prints out information about one of these objects:

    def information(obj):
        print "I'm from {0}, ({1}). I own: {2}".format(
                    obj.city, obj.country, ','.join(obj.things))
    

    Let’s create 2 MyClassOne objects and change one to be Milan, and give Milan “something”:

    foo1 = MyClassOne()
    bar1 = MyClassOne()
    
    foo1.city = "Milan"
    foo1.country = "Italy"
    foo1.things.append("Something")
    

    When we call information() on the foo1 and bar1 we get the values you’d expect:

    >>> information(foo1)
    I'm from Milan, (Italy). I own: Something
    
    >>> information(bar1)
    I'm from Barcelona, (Spain). I own: 
    

    However, if we were to do exactly the same thing, but using instances of MyClassTwo you’ll see that the class attributes are shared between instances.

    foo2 = MyClassTwo()
    bar2 = MyClassTwo()
    
    foo2.city = "Milan"
    foo2.country = "Italy"
    foo2.things.append("Something")
    

    And then call information()…

    >>> information(foo2)
    I'm from Milan, (Italy). I own: Something
    >>> information(bar2)
    I'm from Barcelona, (Spain). I own: Something
    

    So as you can see – things is being shared between the instances. things is a reference to a list that each instance has access to. So if you append to things from any instance that same list will be seen by all other instances.

    The reason you don’t see this behaviour in the string variables is because you are actually assigning a new variable to an instance. In this case that reference is “owned” by the instance and not shared at the class level. To illustrate let’s assign a new list to things for bar2:

    bar2.things = []
    

    This results in:

    >>> information(foo2)
    I'm from Milan, (Italy). I own: Something
    >>> information(bar2)
    I'm from Barcelona, (Spain). I own: 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Instance variables vs. class variables in Python What is the difference between
Possible Duplicate: what is difference between init and call in python? I'm trying to
Possible Duplicate: What is the difference between @staticmethod and @classmethod in Python? I am
Possible Duplicate: In Python, what is the difference between “.append()” and “+= []”? In
Possible Duplicate: python import question What is the difference between importing a Python file
Possible Duplicate: Get difference from 2 lists. Python I have two lists rt =
Possible Duplicate: What is the difference between @staticmethod and @classmethod in Python? I have
Possible Duplicate: Difference between Python Generators vs Iterators Generators seem like big deal in
Possible Duplicate: Python, compute list difference I have two lists For example: A =
Possible Duplicate: Python: Get object by id Typically when you see the string representation

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.