Sunday, 8 May 2022

python private methods, how it differs with public methods best practices

In Python, private methods are methods that are intended to be used only within the class in which they are defined. Private methods are defined by prefixing the method name with two underscores (__) at the beginning of the method name.

Here is an example of a private method in Python:

class MyClass:

    def __init__(self, value):

        self.__value = value


    def __private_method(self):

        print("This is a private method.")


    def public_method(self):

        self.__private_method()



In Above example, we have defined a private method __private_method() in the MyClass class. The method can only be called from within the class.


To call the private method from within the class, we can use the mangled name of the method:

class MyClass: def __init__(self, value): self.__value = value def __private_method(self): print("This is a private method.") def public_method(self): self.__private_method() obj = MyClass(10) obj.public_method() # Output: This is a private method.



we are calling the private method __private_method() from within the public_method() method.

Public methods, on the other hand, are methods that can be called from outside the class. Public methods are defined without any prefixes.

Here is an example of a public method in Python:

class MyClass: def __init__(self, value): self.__value = value def public_method(self): print("This is a public method.") obj = MyClass(10) obj.public_method() # Output: This is a public method.


we have defined a public method public_method() in the MyClass class. The method can be called from outside the class.

private methods in Python are methods that are intended to be used only within the class in which they are defined. Private methods are defined by prefixing the method name with two underscores (__) at the beginning of the method name. Public methods, on the other hand, are methods that can be called from outside the class and are defined without any prefixes.

One important difference between private and public methods is that private methods are not inherited by subclasses. This means that if a class defines a private method, its subclasses cannot use or override the private method. Public methods, on the other hand, are inherited by subclasses and can be used and overridden by subclasses.

Here is an example that demonstrates this difference:


class MyClass: def __private_method(self): print("This is a private method.") def public_method(self): print("This is a public method.") class MySubclass(MyClass): def subclass_method(self): self.public_method() self.__private_method() obj = MySubclass() obj.subclass_method() # Output: This is a public method. AttributeError: 'MySubclass' object has no attribute '_MySubclass__private_method'


we have defined a private method __private_method() and a public method public_method() in the MyClass class. The MySubclass class inherits from the MyClass class and defines a new method subclass_method() that calls both the public and private methods of the parent class.

However, when we run the code, we get an AttributeError because the private method __private_method() cannot be accessed from the subclass.

In general, it is a good practice to use private methods in Python to indicate that they are not intended to be used outside the class. This can help prevent unintended use of the method and make the code more organized. However, it is important to remember that private methods in Python are not truly private and can still be accessed from outside the class if necessary.

It is also worth mentioning that private methods can be useful for implementing class-specific behavior that should not be exposed to the outside world. For example, a private method could be used to perform some internal calculations or to implement some complex logic that should not be visible to the user of the class.

Here is an example that demonstrates the use of private methods in Python:

class Calculator: def __init__(self): self.__result = 0 def add(self, x, y): self.__result = x + y def subtract(self, x, y): self.__result = x - y def __print_result(self): print("The result is:", self.__result) def calculate(self, x, y, operation): if operation == "add": self.add(x, y) elif operation == "subtract": self.subtract(x, y) else: print("Invalid operation.") return self.__print_result() c = Calculator() c.calculate(10, 5, "add") # Output: The result is: 15 c.calculate(10, 5, "subtract") # Output: The result is: 5


In Above example, we have defined a Calculator class that has two public methods add() and subtract() for performing addition and subtraction operations. We have also defined a private method __print_result() that is used to print the result of the operation.

The calculate() method is a public method that takes two operands and an operation as input and performs the specified operation using the add() or subtract() methods. After performing the operation, the calculate() method calls the private __print_result() method to print the result.

private methods in Python are methods that are intended to be used only within the class in which they are defined. Private methods are defined by prefixing the method name with two underscores (__) at the beginning of the method name. Private methods can be useful for implementing class-specific behavior that should not be exposed to the outside world.

Labels: , ,

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home