“Getters et Setters en Python” Réponses codées

Getters et Setters en Python

class Person:
	# Constructor
	def __init__(self, name, age):
    	# Since we making getters and setters, we should mangle the varible names to make them "private" by adding 2 underscores.
    	self.__name = name
        self.__age = age
    
    # Getters
	def get_name(self):
    	return __self.name
	
    def get_age(self):
    	return __self.age
    
    # Setters
    def set_name(self, new_name):
    	self.__name = new_name
    
    def set_age(self, new_age):
    	self.__age = new_age
Liam Welsh

Python Getters et Setters

class Car():
    def __init__(self):
        self._seat = None

    @property # getter
    def seat(self):
        return self._seat

    @seat.setter # setter
    def seat(self, seat):
        self._seat = seat


c = Car()
c.seat = 6
print(c.seat)
Wojak's distant cousin

Réponses similaires à “Getters et Setters en Python”

Questions similaires à “Getters et Setters en Python”

Plus de réponses similaires à “Getters et Setters en Python” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code