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
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).
+ 3
I don't really understand the meaning of your question. Maybe you should post (or better link) your code.
+ 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 ().
+ 2
Yes, getters r not pythonic. +1 for property
0
Omg I forgot to link the code. Sorry, done
0
And what's the difference between the "direct access" and the property method? PS: thank you anyway for the previous answer :)