2

So I was looking at a certain class which has the following property function. However, the property method itself doesn't describe the procedure but instead calls another function to do so as follows:

class Foo():

 @property
 def params(self):
     return self._params()

 @property
 def target(self):
     return self._target()

 def _params(self):
     return print("hello")
 def _target(self):
     return print("world")

What I am trying to understand if it is some sort of pattern? I have seen a similar thing in another class as well where the method with property decorator simply calls another method of same name with underscore in the beginning. Note: I do know what is property decorator but don't understand why this specific way of underscoring aims to achieve.

gfdsal
  • 651
  • 1
  • 8
  • 25
  • try this https://stackoverflow.com/questions/17330160/how-does-the-property-decorator-work – hazirovich Nov 24 '19 at 16:16
  • 1
    @hazirovich im not sure you understood the question well. – gfdsal Nov 24 '19 at 20:21
  • I sometimes uses that if for example `_params` takes a few parameters. I would call it `params` and the property would be a shortcut to the default arguments call. – Cedric H. Dec 15 '19 at 10:38
  • @CedricH. What is the reason for doing so why not simply use params instead of creating another function _params as params is already declared as a property? – gfdsal Dec 15 '19 at 10:42
  • @GENIVI-LEARNER Something like this : create a function `get_information` with parameters with default values, then create a property `information` which calls `get_information` with the default set of parameters. This way users of the class have a shortcut and a better look and feel in some cases. In your specific case, has `_params` and `_target` do not take parameters, I do not see the point. – Cedric H. Dec 16 '19 at 11:04
  • @CedricH.Please elaborate I didnt understand how users of the class have a shortcut and better look and feel. Assuming _params and _target take parameters, how shall it be a shortcut? – gfdsal Dec 16 '19 at 11:43

1 Answers1

1

Effectively, the property is being used as a shortcut for calling a method with a fixed set of arguments. As a slightly different example, consider

class Foo():

    @property
    def params(self):
        return self._params(1, "foo", True)

    def _params(self, x, y, z):
        ...

f = Foo()

Now, f.params is a shortcut for f._params(1, "foo", True). Whether that is worth doing depends on whether _params is used for anything other than implementing the body of the params getter. If it isn't, there's little real point in writing code like this.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • I kinda got it so essentially instead of writing ```f._params(1, "foo", True)``` we can simply do a shortcut such as ```f.params``` without having to specify additional arguments. Right? – gfdsal Dec 19 '19 at 11:24
  • Also could you please elaborate this: **```_params``` is used for anything other than implementing the body of the params getter** – gfdsal Dec 19 '19 at 11:25
  • If the sole purpose of defining `_params` is so that you can call it once from the body of `params`, then you should probably just put its code in `params` directly and not define `_params` in the first place. That would save an unnecessary function call. – chepner Dec 19 '19 at 12:52