Do you use the get/set pattern (in Python)?

后端 未结 8 1964
长情又很酷
长情又很酷 2020-12-12 13:25

Using get/set seems to be a common practice in Java (for various reasons), but I hardly see Python code that uses this.

Why do you use or avoid get/set methods in Py

相关标签:
8条回答
  • 2020-12-12 13:54

    The short answer to your question is no, you should use properties when needed. Ryan Tamyoko provides the long answer in his article Getters/Setters/Fuxors

    The basic value to take away from all this is that you want to strive to make sure every single line of code has some value or meaning to the programmer. Programming languages are for humans, not machines. If you have code that looks like it doesn’t do anything useful, is hard to read, or seems tedious, then chances are good that Python has some language feature that will let you remove it.

    0 讨论(0)
  • 2020-12-12 13:57

    Our teacher showed one example on class explaining when we should use accessor functions.

    class Woman(Human):
        def getAge(self):
            if self.age > 30:
                return super().getAge() - 10
            else:
                return super().getAge()
    
    0 讨论(0)
  • 2020-12-12 13:58

    Cool link: Python is not Java :)

    In Java, you have to use getters and setters because using public fields gives you no opportunity to go back and change your mind later to using getters and setters. So in Java, you might as well get the chore out of the way up front. In Python, this is silly, because you can start with a normal attribute and change your mind at any time, without affecting any clients of the class. So, don't write getters and setters.

    0 讨论(0)
  • 2020-12-12 13:58

    Here is what Guido van Rossum says about that in Masterminds of Programming

    What do you mean by "fighting the language"?

    Guido: That usually means that they're trying to continue their habits that worked well with a different language.

    [...] People will turn everything into a class, and turn every access into an accessor method,
    where that is really not a wise thing to do in Python; you'll have more verbose code that is
    harder to debug and runs a lot slower. You know the expression "You can write FORTRAN in any language?" You can write Java in any language, too.

    0 讨论(0)
  • 2020-12-12 14:04

    In python, you can just access the attribute directly because it is public:

    class MyClass:
    
        def __init__(self):
            self.my_attribute = 0  
    
    my_object = MyClass()
    my_object.my_attribute = 1 # etc.
    

    If you want to do something on access or mutation of the attribute, you can use properties:

    class MyClass:
    
        def __init__(self):
            self._my_attribute = 0
    
        @property
        def my_attribute(self):
            # Do something if you want
            return self._my_attribute
    
        @my_attribute.setter
        def my_attribute(self, value):
            # Do something if you want
            self._my_attribute = value
    

    Crucially, the client code remains the same.

    0 讨论(0)
  • 2020-12-12 14:09

    No, it's unpythonic. The generally accepted way is to use normal data attribute and replace the ones that need more complex get/set logic with properties.

    0 讨论(0)
提交回复
热议问题