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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:08:34+00:00 2026-06-15T04:08:34+00:00

I’m learning Python and I have a question, more theoretical than practical, regarding access

  • 0

I’m learning Python and I have a question, more theoretical than practical, regarding access class variables from method of this class.

For example we have:

class ExampleClass:
    x = 123
    def example_method(self):
        print(self.x)

Why is necessarily to write exactly self.x, not just x? x belongs to namespace of the class, and method using it belongs to it too. What am I missing? What a rationale stands behind such style?

In C++ you can write:

class ExampleClass {
public:
    int x;
    void example_method()
    {
        x = 123;
        cout << x;
    };
};

And it will work!

  • 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-15T04:08:35+00:00Added an answer on June 15, 2026 at 4:08 am

    From The History of Python: Adding Support for User-defined Classes:

    Instead, I decided to give up on the idea of implicit references to
    instance variables. Languages like C++ let you write this->foo to
    explicitly reference the instance variable foo (in case there’s a
    separate local variable foo). Thus, I decided to make such explicit
    references the only way to reference instance variables. In addition,
    I decided that rather than making the current object (“this”) a
    special keyword, I would simply make “this” (or its equivalent) the
    first named argument to a method. Instance variables would just always
    be referenced as attributes of that argument.

    With explicit references, there is no need to have a special syntax
    for method definitions nor do you have to worry about complicated
    semantics concerning variable lookup. Instead, one simply defines a
    function whose first argument corresponds to the instance, which by
    convention is named “self.” For example:

    def spam(self,y):
        print self.x, y
    

    This approach resembles something I had seen in Modula-3, which had
    already provided me with the syntax for import and exception handling.
    Modula-3 doesn’t have classes, but it lets you create record types
    containing fully typed function pointer members that are initialized
    by default to functions defined nearby, and adds syntactic sugar so
    that if x is such a record variable, and m is a function pointer
    member of that record, initialized to function f, then calling
    x.m(args) is equivalent to calling f(x, args). This matches the
    typical implementation of objects and methods, and makes it possible
    to equate instance variables with attributes of the first argument.

    So, stated by the BDFL himself, the only real reason he decided to use explicit self over implicit self is that:

    • it is explicit
    • it is easier to implement, since the lookup must be done at runtime(and not at compile time like other languages) and having implicit self could have increased the complexity(and thus cost) of the lookups.

    Edit: There is also an answer in the Python FAQ.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.