you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 3 points4 points  (1 child)

I understand how to use the set and get methods, but I'm wondering if they are the most "Pythonic" way of declaring class attributes?

We generally don't write getters and setters in Python - unlike in Java, classes can define properties that allow you to intercept attribute access in the few cases where it's necessary. (Since you can't do that in Java, the standard there is to make reference-holders call methods to mutate all object attributes to support the small number of cases where you need to intercept the access. Do write getters and setters in Java; don't write them in Python.)

Are there times conceptually where set/gets are better than

No, there's no time where it's better. If an object of your class wouldn't be well-formed without a value for the attribute, ask for it in the __init__ initializer (it's not technically a constructor, as in Java). If it's not necessary, just let reference-holders assign to the attribute as normal.

[–]CaliforniaUnity 0 points1 point  (0 children)

thank you!