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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:07:30+00:00 2026-06-05T20:07:30+00:00

I started coding in python a week ago, it is my mistake i started

  • 0

I started coding in python a week ago, it is my mistake i started coding using oops,classes and objects that soon. I assumed my C++ proficiency will help…. I got bit by the following code

class A:
     var=0
     list=[]
     def __init__(self):
            pass

Here to my surprise, var and list are kinda global variable, it is shared across all instances it seems…. What I thought was it was different across all the instances….. It took me half a day to figure out that…. It does not make even slightest sense, that a variable can be accessed by a class object only, but is shared across all instances……. Just Curious, is there a reason behind it?????

  • 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-05T20:07:31+00:00Added an answer on June 5, 2026 at 8:07 pm

    var should definitely not be shared as long as you access it by instance.var or self.var. With the list however, what your statement does is when the class gets evaluated, one list instance is created and bound to the class dict, hence all instances will have the same list. Whenever you set instance.list = somethingelse resp. self.list = somethingelse, it should get an instance level value.

    Example time:

    >>> class A():
    ...     var = 0
    ...     list = []
    ...
    >>> a = A()
    >>> b = A()
    >>> a.var
    0
    >>> a.list
    []
    >>> b.var
    0
    >>> b.list
    []
    >>> a.var = 1
    >>> b.var
    0
    >>> a.list.append('hello')
    >>> b.list
    ['hello']
    >>> b.list = ['newlist']
    >>> a.list
    ['hello']
    >>> b.list
    ['newlist']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn the python scripting . Being some body who started coding
Today I started coding a project that uses sqlite and when I tried to
I have a feeling this will be a quick fix, given that I started
Not long ago I started programming Python with Django. Sometimes I get strange errors
I started coding an RPG engine in python and I want it to be
I've recently started doing some coding in Python again and I've encountered an error
I started coding with groovy today and I notices that if I take the
I just started coding a gtalk chat bot using libjingle. I'm having a problem
Well i started coding a mini MVC base that I can just use on
I recently started learning Python. Not yet ventured into coding. During one of my

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.