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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:19:18+00:00 2026-06-12T13:19:18+00:00

What is the difference between creating a variable using the self.variable syntax and creating

  • 0

What is the difference between creating a variable using the self.variable syntax and creating one without?

I was testing it out and I can still access both from an instance:

class TestClass(object):
    j = 10
    def __init__(self):
        self.i = 20

if __name__ == '__main__':
    testInstance = TestClass()
    print testInstance.i
    print testInstance.j

However, if I swap the location of the self, it results in an error.

class TestClass(object):
    self.j = 10
    def __init__(self):
        i = 20

if __name__ == '__main__':
    testInstance = TestClass()
    print testInstance.i
    print testInstance.j  

>>NameError: name 'self' is not defined

So I gather that self has a special role in initialization.. but, I just don’t quite get what it is.

  • 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-12T13:19:19+00:00Added an answer on June 12, 2026 at 1:19 pm

    self refers to the current instance of the class. If you declare a variable outside of a function body, you’re referring to the class itself, not an instance, and thus all instances of the class will share the same value for that attribute.

    In addition, variables declared as part of the class (rather than part of an instance) can be accessed as part of the class itself:

    class Foo(object):
        a = 1
    
    one = Foo()
    two = Foo()
    
    Foo.a = 3
    

    Since this value is class-wide, not only can you read it directly from the class:

    print Foo.a # prints 3
    

    But it will also change the value for every instance of the class:

    print one.a # prints 3
    print two.a # prints 3
    

    Note, however, that this is only the case if you don’t override a class variable with an instance variable. For instance, if you created the following:

    class Bar(object)
        a = 1
        def __init__(self):
            self.a = 2
    

    and then did the following:

    one = Bar()
    two = Bar()
    two.a = 3
    

    Then you’d get the following results:

    print Bar.a # prints "1"
    print one.a # prints "2"
    print two.a # prints "3"
    

    As noted in the comments, assigning to two.a creates an instance-local entry on that instance, which overrides the a from Bar, hence why Bar.a is still 1 but two.a is 3.

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

Sidebar

Related Questions

Is there any tangible difference between the two forms of syntax available for creating
Can any one explain difference between position and anchor point in cocos-2D with some
What is the difference between creating a thead using BackgroundWorker and creating a thread
What is the difference between creating one index across multiple columns versus creating multiple
What is the difference between creating an event using initEvent and later dispatching the
Is there any difference between following two ways of creating an object. Student s1
What is the advantages of creating hierarchy in SSAS? What are the difference between
Difference between start-pointers and interior-pointers and in what situation we should prefer one over
The difference between Chr and Char when used in converting types is that one
It seems to me that in Sage the only difference between creating a dense

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.