Public, Private and Protected Methods in Python


In Python, we can differentiate between public, private, and protected methods based on their access level.

  • Public methods are those that can be accessed from anywhere within or outside the class.
  • Private methods in a Python's class are those that can only be accessed from inside the class.
  • Protected methods are those that can only be accessed from inside the class and its subclasses.


Public Methods

Public methods are accessible from anywhere within or outside the class. They play a significant role in interacting with the class's attributes and functionality. When developers create a method without any underscore prefix, it automatically becomes a public method.


Private Methods

Private methods in Python are designed to be accessed only from within the class in which they are defined. They are indicated by prefixing the method name with double underscores "__".


Protected Methods

Protected methods are indicated by prefixing the method name with a single underscore "_". They can be accessed from within the class itself and its subclasses.


date:July 3, 2024