Dec-07-2017, 03:03 AM
This is not Java. As far as python goes, we don't code this way. Unless a getter/setter actually does work, you don't write one.
So your car should just be:
Python has a "we are all consenting adults" philosophy on data access. There are no private/public attributes so not only is writing getters/setters not recommended; it is actually pointless. I can access all those attributes if I want to.
Basically, Python doesn't enforce encapsulation (and if you are trying to enforce it you are writing bad python). You still use encapsulation by coding responsibly.
So your car should just be:
class Car: def __init__(self, ID, name, tire, engine): self.ID = ID self.name = name self.tire = tire self.engine = engine def __str__(self): template = "ID: {ID} Name: {name} Tire: {tire} Engine: {engine}" return template.format(**vars(self))If someone wants to access an attribute of tire they just do so using
mycar.tire.name
for example.Python has a "we are all consenting adults" philosophy on data access. There are no private/public attributes so not only is writing getters/setters not recommended; it is actually pointless. I can access all those attributes if I want to.
Basically, Python doesn't enforce encapsulation (and if you are trying to enforce it you are writing bad python). You still use encapsulation by coding responsibly.