Sitemap

Python: _ vs __ in Python Encapsulation

Aug 16, 2024

--

Python

Press enter or click to view image in full size

_ :

  • protected variable/method
  • iska matlab hai ye variable/method internal use ke liye hai, yaani ki developers ke liye
  • example,
self._protected_variable
  • more,

Get Shahwar Alam Naqvi’s stories in your inbox

Join Medium for free to get updates from this writer.

class MyClass:
def __init__(self):
self._protected_variable = 10

def _protected_method(self):
pass

__ :

  • private variable/method
  • iska matlab hai ye strictly private hai, ye bilkul bhi class se bahar accessible nahi hona chahiye
  • example,
self.__private_variable
  • more,
class MyClass:
def __init__(self):
self.__private_variable = 20

def __private_method(self):
pass

Thanks.

--

--