0

I am learning to use Python with OOP and when reviewing the concept of encapsulation (define the degree of access and modification of attributes and methods of a class - I understand this concept in this way) I have found information in which it tells me that to define methods and attributes as protected and private you must use "_" and "__" when naming them. for example:

class Mueble:

    _marca = "Tecnot"

    def __init__(self, tipo, marca, material):
        self.tipo = tipo # atributo de tipo público
        self._marca = marca # atributo de tipo protegido (solo se puede acceder desde la clase y sus clases hijas)
        self.__material  = material # atributo de tipo privado (solo se puede acceder desde la clase)

    def mostrar_atrib_proteg(self): # método público
        return self._marca

    def _desarmar(self): # método protegido
        print("Se ha desarmado el mueble")

    def __romper(self): # método privado
        print("Se ha destruido el mueble")


primerMueble = Mueble("silla", "Duramax", "caoba")
print("propiedad pública", primerMueble.tipo) # mostrando un atributo público
print("propiedad protegida", primerMueble._marca) # mostrando un atributo protegido
print("accediendo a la propiedad privada", primerMueble._Mueble__material) # mostrando un atributo privado (a través de la clase)

primerMueble.mostrar_atrib_proteg() # ejecutando un método publico
primerMueble._desarmar() # ejecutando un método protegido
primerMueble._Mueble__romper() # ejecutando un método privado (a través de la clase)

When trying to test the access to the methods and attributes I realized that I could still enter so I looked for information about this and found links like this and this one that indicated that the way to define attributes and methods as protected and private was the way I was using but I found this link (it's in Spanish, my language) in which they explained with examples that the concepts of private and protected attributes do not exist in Python.

At the same time, I have seen that there are questions related to my doubt here and here. And when reviewing the answer to the first one, I see that it is indicated that the concept of private and protected but in the answer (and the question) they present a way defined by the language of how to define something as private.

My question is, how is the concept of encapsulation raised in Python 3?

Obs: I have tried to search the Python 3 documentation for a reference to these concepts but I have not been able to find something that clarifies my doubt.

Diego Aaron
  • 386
  • 1
  • 6
  • 19
  • Python doesn't really support a concept of strictly enforced encapsulation. The solution that's used in practice is to incorporate static validation into your tests that will fail if encapsulation is broken -- `pylint` has a check for this, for example. https://docs.quantifiedcode.com/python-anti-patterns/correctness/accessing_a_protected_member_from_outside_the_class.html – Samwise Oct 28 '20 at 19:01
  • Aslo check this [How to do encapsulation in Python?](https://stackoverflow.com/questions/26216563/how-to-do-encapsulation-in-python) - related code https://gist.github.com/sloanlance/c65ae4f4197f773e57ce13c1f9b64fd1 – dmitryro Oct 28 '20 at 19:07
  • 2
    **Python does not have access modifiers**. Instead, convention is used. A *single* underscore signals a "private" variable. There is no "protected". Double-underscores != private. Double-underscores are for *name mangling*, the sole purpose of which is to prevent name collisions in subclasses. – juanpa.arrivillaga Oct 28 '20 at 20:03
  • More importantly, access modifiers *are not synonymous with encapsulation*. The OOP concept of encapsulation basically means "bundle private state with methods that act on that state". It is closely related to "information hiding", which basically means that the users of your interface do not need to understand the internals of the implementation. To drive a car, you shouldn't have to understand the mechanisms of an internal combustion engine, instead, you define an interface (e.g. an acceleration pedal). Then, if you switch to an electric engine, people can still use your car. – juanpa.arrivillaga Oct 28 '20 at 20:07
  • Access modifiers are a language construct that is meant to *encourage encapsualtion/information hiding* in the languages that support it. However, your typical Java Bean, with a bunch of private attributes and public getters and setters can still totally violate the *principle* of encapsulation/data hiding. This is fundamentally about *how you design your APIs*. – juanpa.arrivillaga Oct 28 '20 at 20:08

0 Answers0