4

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 it to work. Here's my code:

class Car:
    def __init__(self, carMake, yrMod):
        self.__make = carMake
        self.__yearModel = yrMod
        self.__speed = 0

    #Mutator Methods
    def set_make(self, make):
        self.__make = carMake

    def set_model(self, yrMod):
        self.__yearModel = yrMod

    #def set_speed(self, speed):
        #self.__speed = speed

    #Accessor Methods
    def get_make(self):
        return self.__make

    def get_yearModel(self):
        return self.__yearModel

    def get_speed(self):
        return self.__speed

myCar=Car('Ford', 1968)
myCar2=Car('Nissan', 2012)
myCar.get_make()
'Ford'
myCar.set_make=('Porche')
myCar.get_make()
'Ford'

Why doesn't myCar.set_make change Ford into Porche? Thank you.

A. Rodas
  • 20,171
  • 8
  • 62
  • 72
user2255444
  • 43
  • 1
  • 1
  • 3
  • 2
    What's with all these underscores in your instance attributes, e.g. `self.__speed`? And why are you making all these methods anyhow? What advantage do you think `myCar.set_make("Porsche")/myCar.get_make()` will offer over `myCar.make = 'Porsche'` and `myCar.make`? – DSM Apr 07 '13 at 20:56
  • 3
    There is never a reason to expose an API like this. There is sometimes a reason to have getter/setter methods, but they should be hidden using the built-in `property`. And since that makes them like normal attributes from the client code's perspective, you should just use those (rather than trivial getters/setters) until you need logic. –  Apr 07 '13 at 20:57

2 Answers2

9

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:

def set_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.

Community
  • 1
  • 1
A. Rodas
  • 20,171
  • 8
  • 62
  • 72
  • 3
    There's also a bug in `set_make` (`carMake` instead of `make`) so it won't quite work yet, but you're right about the main problem. – DSM Apr 07 '13 at 20:57
  • Thank you for that, but for some reason I get an error message: – user2255444 Apr 07 '13 at 20:57
  • This was my first time using this resource, I just started an intro to programming class, so thank you for your patience! – user2255444 Apr 07 '13 at 21:16
-1
new_car = Car(...)
new_car2 = Car(...)

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

print new_car._make
print new_car2.make
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • 2
    And what value does this answer add to the question? Since you took the time to post this as an answer to a year old question I'm curious. – EWit Aug 18 '14 at 22:29