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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:59:35+00:00 2026-06-18T06:59:35+00:00

I want to know what is the difference between these in Python? self._var1 self._var1_

  • 0

I want to know what is the difference between these in Python?

self._var1
self._var1_
self.__var1
self.__var1__
  • 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-18T06:59:37+00:00Added an answer on June 18, 2026 at 6:59 am

    As a starting point, you will probably find helpful this quote from PEP 8 – Style Guide For Python Code:

    In addition, the following special forms using leading or trailing
    underscores are recognized (these can generally be combined with any
    case convention):

    _single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose name starts with an underscore.

    single_trailing_underscore_: used by convention to avoid conflicts
    with Python keyword, e.g.
    Tkinter.Toplevel(master, class_='ClassName')

    __double_leading_underscore: when naming a class attribute, invokes name mangling (inside class FooBar, __boo becomes _FooBar__boo; see
    below).

    __double_leading_and_trailing_underscore__: "magic" objects or attributes that live in user-controlled namespaces. E.g. __init__,
    __import__ or __file__. Never invent such names; only use them as documented.

    You asked in the context of class attributes, though, so let’s take a look at your specific examples:

    Single leading underscore

    Naming an attribute in your class self._var1 indicates to the user of the class that the attribute should only be accessed by the class’s internals (or perhaps those of a subclass) and that they need not directly access it and probably shouldn’t modify it. You should use leading underscores in the same places that you would use a private or protected field in Java or C#, but be aware that the language doesn’t actually enforce non-access – instead you trust your class’s user to not do anything stupid, and leave them the option of accessing (or modifying) your class’s private field if they’re really, really sure that they know what they’re doing and it makes sense.

    Single leading and trailing underscore

    self._var1_ isn’t something I’ve ever seen. I don’t think this naming style has any conventional meaning in the Python world.

    Double leading underscore

    This one actually has syntactical significance. Referring to self.__var1 from within the scope of your class invokes name mangling. From outside your class, the variable will appear to be at self._YourClassName__var1 instead of self.__var1. Not everyone uses this – we don’t at all where I work – and for simple classes it feels like a slightly absurd and irritating alternative to using a single leading underscore.

    However, there is a justification for it existing; if you’re using lots of inheritance, if you only use single leading underscores then you don’t have a way of indicating to somebody reading your code the difference between ‘private’ and ‘protected’ variables – ones that aren’t even meant to be accessed by subclasses, and ones that subclasses may access but that the outside world may not. Using a single leading underscore to mean ‘protected’ and a double underscore to mean ‘private’ may therefore be a useful convention in this situation (and the name mangling will allow a subclasses to use a variable with the same name in their subclass without causing a collision).

    Double leading and trailing underscore

    self.__var1__ is something you should never create as I’ve literally written it, because the double leading and trailing underscore naming style is meant to be used only for names that have a special meaning defined by Python, like the __init__ or __eq__ methods of classes. You’re free to override those to change your class’s behavior (indeed, almost all classes will have a programmer-defined __init__), but you shouldn’t make up your own names in this style like self.__var1__.

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

Sidebar

Related Questions

I want to know what is the basic difference between these to files -
I just want to know what is the difference between these two messaging constants.
I want to know the difference between these two statements.... ch=getch(); ch=(*getch)(); both statements
I want to know the difference between these two statements. Is one 'better' than
I just want to know what is the difference between all these annotations. Why
I want to know what the difference is between these two regular expressions, what
I want to know technical difference between WebDriver Wait timeout and implicitlyWait timeout.
I want to know the difference between : $this->forward(module, action); And $this->redirect(module/action); My first
I just want to know the difference between List<string> lst = new List() and
As title suggests I want to know the difference between the change and click

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.