Hi everyone I hope this message finds you well. In fact I want to talk about a subject that is focused on encapsulation in python with access levels as in other languages like C++, Java, and C# …, in fact there is a principle on encapsulation in python that is secured by convention but not by use. Lately I have been working in fact I was just having fun writing python code in OOP on the encapsulation part with the basic attributes at the level of the nomenclature, the _ refers to a protected attribute or method __ private attribute or method according to the convention I must not access the attributes __ and _ outside the class the _ we do it but it is not recommended. But afterwards I see that even with the __ I can have access outside the class I just have to go through the class itself in order to have access to this attribute and so I told myself that it is not secure. So I thought of making a more elegant and organized way which was to make a restriction on the access of these attributes outside the class; the convention is one thing but to the extent that an unconscious dev takes your code he can do without the mangling system so I made another combination of code using existing python code in order to bring a more voluminous encapsulation layer. Not only based on the convention, but that we block access to the resource when its access is not authorized and I produced this code. Now I want to ask you what do you think of this code? start of the code:
#Started since 02-10-2024
import datetime as date
class Vehicule:
“”"
Classe pour tester les attributs privés, les conventions Python
et l’usage de @property pour contrôler l’accès.
“”"
def init(self, mark, speed, engin, people_name, year_made=None):
self.__mark = mark
self.__speed = speed
self.__engin = engin
self.__year_made = year_made if year_made is not None else self.__generate_year_made()
self._people_name = people_name
# generation de la date pour chaque instance
def __generate_year_made(self):
create_at = date.datetime.now()
return create_at.strftime("%Y-%m-%d")
""" Ma combinaison magique"""
@property
def engin(self):
return self.__engin
@engin.setter
def engin(self, new_engin_by_user):
if not isinstance(new_engin_by_user, str):
raise ValueError("Vous devez saisir une chaîne de caractères pour l'engin.")
self.__engin = new_engin_by_user
@property
def mark(self):
return self.__mark
@mark.setter
def mark(self, new_mark_user):
if not isinstance(new_mark_user, str):
raise ValueError("Vous devez saisir une chaîne de caractères pour le mark.")
self.__mark = new_mark_user
@property
def speed(self):
return self.__speed
@speed.setter
def speed(self, new_speed):
if not isinstance(new_speed, int):
raise ValueError("Vous devez saisir un entier pour la vitesse.")
if new_speed <= 0 or new_speed > 300:
raise ValueError("La vitesse doit être comprise entre 1 et 300.")
self.__speed = new_speed
@property
def year_made(self):
return self.__year_made
@year_made.setter
def year_made(self, _):
raise AttributeError("La modification de l'année de création est interdite !")
@property
def people_name(self):
return self._people_name
@people_name.setter
def people_name(self, new_people_name_user):
if not isinstance(new_people_name_user, str):
raise ValueError("Le format du nom est invalide, veuillez saisir un nom valide.")
self._people_name = new_people_name_user
main prog
if name == ‘main’:
vehicule1 = Vehicule(‘RANGE ROVER’, 250, ‘Range VVS 456’, ‘Fodé Kerfala Camara’)
print(“Nom du propriétaire:”, vehicule1.people_name)
print(“Marque:”, vehicule1.mark)
print(“Vitesse:”, vehicule1.speed)
print(“Année de création:”, vehicule1.year_made)
#End 08-11-2024