Python Forum

Full Version: Python class noob
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi I need help I want my code to make it so it does print the message but it dosent seem to work.

class Bil:
    __modelYear = ()  # private
    Status = ()

    def set_modelYear(self, Year):
        self.__modelYear = Year

    def get_modelYear(self):
        return self.__modelYear


def main():
    fiat = Bil()
    fiat.set_modelYear(2017)
    print('Har nu skaffat mig en bil av ', fiat.get_modelYear(), 'års modell')
you didn't call main
main()
Private Hand not in Python,and use of getters/setters not at all for simple attribute access.
Then it can look this.
class Bil:
    def __init__(self, model, year):
        self.model = model
        self.year = year

car = Bil('Fiat', 2017)
print(f'Har nu skaffat mig en {car.model} av {car.year} års modell')
# Setter(The Python way)
car.year = 2020
print(f'Har nu skaffat mig en {car.model} av {car.year} års modell')
Output:
Har nu skaffat mig en Fiat av 2017 års modell Har nu skaffat mig en Fiat av 2020 års modell