[PYTHON] setter and getter

class Car(object):
    def __init__(self,model=None):
        self.model=model

    def run(self):
        print('run')


class ToyotaCar(Car):
    def __init__(self, model,enable_auto_run = False):
        #self.model =It can also be written as a model, but the parent method can be read using super.
        super().__init__(model)
        #If you want to access the variable using getter or setter, before the variable name_Put on
        self._enable_auto_run = enable_auto_run
        #Before the variable name to completely shield from the outside_Attach two
        #self.__enable_auto_run = enable_auto_run

    @property
    def enable_auto_run(self):
        return self._enable_auto_run

    @enable_auto_run.setter
    def enable_auto_run(self,is_enable):
        self._enable_auto_run = is_enable

    #Method override
    def run(self):
        print('fast fun')


car = Car()
car.run()

toyotar_car = ToyotaCar('Lexus')
toyotar_car.enable_auto_run = True
print(toyotar_car.enable_auto_run)

run
True

Recommended Posts

setter and getter
setter, getter
Automatically create Java getter / setter definitions
About _ and __
Precautions when adding setter and deleter in derived class to getter property of base class