Suppose I have:
class myclass:
def __init__(self):
self.foo = "bar"
where the value of foo needs to be available to users of myclass. Is it OK to just read the value of foo directly from an instance of myclass? Should I add a get_foo method to myclass or perhaps add a foo property? What's the best practice here?
The applicable Python maxim would be "we're all adults here" - if users need direct access to the value of foo
, let them access it directly. A getter or property would make sense if you need to run some code when it's accessed, otherwise the direct way is best.
Also, you can always transparently turn it into a property later if you need to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With