Usage of __ while declaring any variables or class member in python

Is it good practice to use __ convention while declaring the member variable?

This also imparts private kind of feature of that data member. There have been cases when I found that its good to have hidden members of a class. This is certainly exposes only that feature that will help the the object to behave in more protected manner.

A lot of people erroneously use double underscores to simulate “private” members, because double underscores invokes code mangling and makes those members harder to reference outside the class. However, it does not actually make them inaccessible.

Most of the time, it mainly adds a road bump to unit testing.

Really the double underscore mangling mechanism is to hide those members from subclasses that you don’t want clobbering the values inadvertently. Name mangling isn’t intended to hide the member from other programmers; the mangling scheme is simple, and referencing the variables anyway is easy.

Single underscore is the common convention for internal members. That’s saying, we’re all adults here, and although you can see it, this variable is intended for internal use. If you reference it, there’s no guarantee it will still be there in future versions.

The Pep8 doc talks about that, and says use of the double underscores for variables and functions should really be rare.

The feature is specifically meant for preventing accidental clashes between a parent class member and subclasses.

The name is mangled by prepending it with the class name; the atribute __foo on a class Bar becomes _Bar__foo, and can still be accessed as such from outside.

This makes such attributes obscured at best, not private. The only real use-case for such attributes is for member attributes that are tied to the specific implementation of a parent class method that a subclass might accidentally overwrite with their own version.

1

Adding to Rob Y’s answer: many think that using the double underscore prevents the method from getting overridden, but that is not true:

class Me(object):
    def override_me(self):
        print "Me: I should NOT be called"

    def __dont_override_me(self):
        print "Me: I SHOULD be called"

class OverrideMe(Me):
    def override_me(self):
        print "OverrideMe: I SHOULD be called"

    def __dont_override_me(self):
        print "OverrideMe: I should NOT be called"


me = Me()
override = OverrideMe()

me._Me__dont_override_me()
override.override_me()
override._OverrideMe__dont_override_me()

Output:

Me: I SHOULD be called
OverrideMe: I SHOULD be called
OverrideMe: I should NOT be called

It depends on how you access it, and has nothing to do with preventing overriding.

However, name mangling will prevent me.__dont_override_me() to be called, as it will throw an attribute error:

me.__dont_override_me()

Output:

Traceback (most recent call last):
  File "/private/tmp/stackoverflow.py", line 23, in <module>
    me.__dont_override_me()
AttributeError: 'Me' object has no attribute '__dont_override_me'

1

The comment made by Alex Punnen is incorrect, but unluckily I don’t have the reputation point to add comment there. What he tries to do is to abuse the “grown man” assumption of Python to directly call the mangled methods, which is not what is expected to be done anyway. In any case, if you write a new __dont_override_me method in a subclass, you don’t get overriding. You get overloading instead: an unrelated method with the same name. The key difference is that the “old” code, i.e., code in the base class Me, will not (normally, unless you use the mangled name directly) call that new method. In his example:

class Me(object):
    def override_me(self):
        print "Me: I should NOT be called"

    def not_overriden(self):
        self.__dont_override_me()

    def __dont_override_me(self):
        print "Me: I SHOULD be called"

class OverrideMe(Me):
    def override_me(self):
        print "OverrideMe: I SHOULD be called"

    def __dont_override_me(self):
        print "OverrideMe: I should NOT be called"

me = Me()
override = OverrideMe()

me.override_me()
me.not_overriden()
override.override_me()
override.not_overriden()

This runs with the following result:

Me: I should NOT be called
Me: I SHOULD be called
OverrideMe: I SHOULD be called
Me: I SHOULD be called

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật