-4

I am converting Java program to python. here i am stuck at one place ie getter and setter function. the java code is following, i have to convert it into python code.

public String getABC() {
    return ABC; 
}
public void setABC(String ABC) {
    this.ABC = ABC;
}
Avi
  • 7
  • 1
    Have you done any research at all? If you are already stuck at getters and setters, I recommend you stop right there. – f1sh Aug 06 '20 at 11:47
  • 1
    wow nice encouraging statement. Really appreciated. thankx – Avi Aug 06 '20 at 11:52
  • 3
    unfortunately, googling is a huge part of coding. I am merely pointing out that you did not research anything at all, otherwise you would have found plenty of results like [this](https://stackoverflow.com/questions/2627002/whats-the-pythonic-way-to-use-getters-and-setters). – f1sh Aug 06 '20 at 11:56
  • Don't use getters and setters in Python – juanpa.arrivillaga Aug 06 '20 at 11:58
  • 1
    Does this answer your question? [What's the pythonic way to use getters and setters?](https://stackoverflow.com/questions/2627002/whats-the-pythonic-way-to-use-getters-and-setters) – Scratte Aug 07 '20 at 06:44

1 Answers1

2

Python also has a property getter/setter mechanism:

class SomeClass:
    def __init__(self):
        self._abc = None

    @property
    def abc(self):
        return self._abc

    @abc.setter
    def abc(self, value):
        self._abc = value


obj = SomeClass()
obj.abc = 'test'
print(obj.abc)  # "test"

But it's worth noting that this approach would make sense only if you need to control access to a protected property or to perform additional operations while getting or setting the value. Otherwise, it would be more straightforward to initialise a property in the constructor and use it directly:

class SomeClass:
    def __init__(self):
        self.abc = None

obj = SomeClass()
obj.abc = 'test'
print(obj.abc)  # "test"

This tutorial should help you: https://www.python-course.eu/python3_properties.php.

VisioN
  • 143,310
  • 32
  • 282
  • 281
  • you really shouldn't use property like this, the whole point is to avoid writing pointless getters and setters – juanpa.arrivillaga Aug 06 '20 at 11:58
  • @juanpa.arrivillaga The whole point of what? From that perspective, you don't need to define custom classes: just use a SimpleNamespace and monkey patch everything you want. I'm personally not a big fan of it. Property getters and setters are there for a reason, some may find is convenient, especially those who natually got used to it (like with Java background). – VisioN Aug 06 '20 at 12:01
  • No, writing a property that simply gets and sets and attribute *is completely pointless*. It's not convenient, it's the opposite of convenient, it's code that does nothing useful. The *whole point* is to allow you to have encapsulation without writing boilerplate getters and setters. That's the reason it's there. Later, if you decide to control access, you can add a property (or other descriptor) without breaking the rest of the code that relied on `self.foo` and `self.foo = val`... that's the *reason* we don't just write `def get_foo(self)` and `def set_foo(self, value)` in python. – juanpa.arrivillaga Aug 06 '20 at 12:04
  • So, idiomatically, that class you have above would only have `self.abc = None` in `__init__` with no getters and setters. – juanpa.arrivillaga Aug 06 '20 at 12:04
  • @juanpa.arrivillaga Yes, but that was exactly the example of encapsulation in Python, isn't it? In Java you can also have public properties and access them directly without getters and setters. Same here, so I see the provided use case completely legit, which clearly mirrors the functionality expected by the OP. – VisioN Aug 06 '20 at 12:10
  • No, you aren't getting it. If you remove those properties, your class is *exactly as encapsulated* as with the property. The property like this does nothing, seriously, this is very unidiomatic. You only add a property if you want your getter, setter, or deleter *to actually do something*. It's like writing a method that *only* calls the super-class method, and does nothing else. The whole point of `property` is *encapsulation **without** boilerplate getters and setters* like you must do in Java, which doesn't have descriptors – juanpa.arrivillaga Aug 06 '20 at 12:14
  • @juanpa.arrivillaga I am getting it and I suppose the provided code was given just for example to show how this approach can be implemented in Python. I assume in Java the reason behind using getters and setters nowadays is also not only to "hide" class properties but also to perform additional operations inside getters and setters. – VisioN Aug 06 '20 at 12:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219331/discussion-between-juanpa-arrivillaga-and-vision). – juanpa.arrivillaga Aug 06 '20 at 12:22