0

Why the print function doesn't return the name of the object?

I'm new to OOP in Python, but I used Java before, and it's really different! I don't understand why when I use a getter method it should return the name of the object (esame) but it doesn't, while when I call directly the variable with 'myobject.name' it works properly. Any help? https://code.sololearn.com/c8I39D3wIK1y/?ref=app

6th May 2019, 8:16 PM
Giorgio Mannarini
Giorgio Mannarini - avatar
6 Answers
+ 3
I wrote some lines of code for better understanding: https://code.sololearn.com/cKcc6g3Q6max/#py As Anna already wrote, @property is used to create read only attributes. Python being Python means that those attributes can still be accessed though. You can't create truly private attributes on Python (see the code I linked).
6th May 2019, 10:34 PM
Thoq!
Thoq! - avatar
+ 3
I don't really understand the meaning of your question. Maybe you should post (or better link) your code.
6th May 2019, 8:50 PM
Thoq!
Thoq! - avatar
+ 3
Either use print(i.getNomeEsame()) to actually execute the getter method or decorate it like this: @property def getNomeEsame(self): return self.nome to create a read-only property that can be called without ().
6th May 2019, 9:18 PM
Anna
Anna - avatar
+ 2
Yes, getters r not pythonic. +1 for property
6th May 2019, 9:51 PM
Thoq!
Thoq! - avatar
0
Omg I forgot to link the code. Sorry, done
6th May 2019, 9:02 PM
Giorgio Mannarini
Giorgio Mannarini - avatar
0
And what's the difference between the "direct access" and the property method? PS: thank you anyway for the previous answer :)
6th May 2019, 10:01 PM
Giorgio Mannarini
Giorgio Mannarini - avatar