Skip to content Skip to sidebar Skip to footer

Accessor & Mutator Methods (python)

I am trying to figure out encapsulation in Python. I was doing a simple little test in shell to see how something worked and it doesn't work like I was expecting. And I can't get

Solution 1:

With myCar.set_make=('Porche'), you are setting this member the Car class as the 'Porche' string, but you are not calling the method.

Just remove the = to solve it:

myCar.set_make('Porche')
myCar.get_make() # Porche

Besides, as @DSM points out, there is an error in the argument of set_make:

defset_make(self, make):
    self.__make = make # carMake is not defined!

However, this use of getters and setters in Python is strongly discouraged. If you need something similar for any reason, consider using properties.

Solution 2:

new_car = Car(...)
new_car2 = Car(...)

new_car._make = 'Ford'
new_car2.make = 'Jetta'print new_car._make
print new_car2.make

Post a Comment for "Accessor & Mutator Methods (python)"