Python Programming - Encapsulation - Discussion

Discussion Forum : Encapsulation - General Questions (Q.No. 96)
96.
How can encapsulation be enforced in Python to make a variable 'product_code' accessible only within its own class?
class Product:
    def __init__(self, product_code, __price):
        self.product_code = product_code
        self.__price = __price
Use a getter method for 'product_code'
Add a single underscore prefix before 'product_code'
Make 'product_code' a global variable
Add a double underscore prefix before 'product_code'
Answer: Option
Explanation:
Adding a double underscore prefix before 'product_code' makes it a private variable, enforcing encapsulation within the class.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.