-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.

Joachim Sauer
11k3 gold badges54 silver badges45 bronze badges
asked Oct 31, 2012 at 10:42
1

1 Answer 1

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.

answered Oct 31, 2012 at 11:09
1
  • 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. Commented Nov 15, 2012 at 13:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.