-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
  • 10,956
  • 3
  • 52
  • 45
cobie
  • 3,237
  • 5
  • 23
  • 21
  • Public properties are more often considered a violation of encapsulation rather than abstraction. It's unlikely that the change you posit would make sense without more changes to the API (e.g. a change to the property or method name at the least) of the object in question. Possible duplicates: http://programmers.stackexchange.com/questions/144347/when-or-why-should-one-use-getters-setters-for-class-properties-instead-of-simpl, http://programmers.stackexchange.com/questions/21802/when-are-getters-and-setters-justified – Mike Partridge Oct 31 '12 at 12:43

1 Answers1

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.

Martijn Verburg
  • 22,006
  • 1
  • 49
  • 81
  • 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