Python Forum
Python class noob - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python class noob (/thread-29741.html)



Python class noob - nallster7 - Sep-17-2020

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')



RE: Python class noob - Yoriz - Sep-17-2020

you didn't call main
main()



RE: Python class noob - snippsat - Sep-17-2020

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