-1

I have been programming in python and Java for quite a number of years and one thing I find myself doing is using the setters and getters from Java in Python but a number of blogs seem to think using the dot notation for access is the pythonic way.

What I would like to know is if using dot to access methods does not violate abstraction principle?

If for example I implement an attribute as a single object and use dot notation to access, if I wanted to change the code later so that the attribute is represented by a list of objects, that would require quite some heavy lifting which violates abstraction principle.

1

It depends :-). If you treat the . notation as a shortcut for a public getter, then personally I have no issue with that. However, having everything publicly accessible via the . notation by default in my opinion is a mistake. Not all fields should be automatically visible to the outside world.

  • and if you don't mind refactoring and recompiling in unlikely event of introducing actual getter. Sometimes its ok (you are only consumer of such object), but sometimes you don't even know who is using previous version. Not needed mutability is bigger issue compared to imagined vs actual getter and setter. For final imutable/primitive fields (of value objects) it is mostly good to make them public. – user470365 Nov 15 '12 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.