-1

I am trying to figure out how the property decorator works in Python.

So,

 class Foo:

 def method(self,param):
    self._method(param)

 @property
 def _method(self): # works fine.. why... I havent defined this to accept any argument
    #some logic
    #even though it passes.. how do i retrieve "param" parameter passed above?

vs.:

 class Foo:

 def method(self,param):
    self._method(param)

 # no property decorator here (throws error)
 def _method(self):
    #some logic

If the decorator property is removed, Python throws an error, which is fine, as I am passing an argument which is not accepted in the method. I understand that, but why doesn't _method(self) need any more params with property defined?

And how do I retrieve the param value inside method using decorator approach?

frazman
  • 32,081
  • 75
  • 184
  • 269
  • Rename it to `@property def _method(x)` and see if it still works – Tim Jul 02 '14 at 22:06
  • @TimCastelijns: But thats not what I want to know.. What I want to know is why does it succeeds.. and how do i retreive that parameter? – frazman Jul 02 '14 at 22:08
  • 1
    Why do you want `_method` to be a `@property`? Why not leave it as a method, and define the parameter you want `def _method(self, param):`? What are you trying to achieve? It would generally help if you provided code that runs and a precise definition of the problem (an error - where's the traceback). – jonrsharpe Jul 02 '14 at 22:13
  • I am trying to understand why this particular code works.. – frazman Jul 02 '14 at 22:14
  • But you're saying it doesn't work - you either get an error, or can't access the parameter. Again, *what are you trying to achieve*?! – jonrsharpe Jul 02 '14 at 22:15
  • Yeah.. I mean, why does adding a decorator "property" makes the function definition list not to have the argument list.. – frazman Jul 02 '14 at 22:16
  • I am just trying to understand the concept.. really – frazman Jul 02 '14 at 22:16
  • Could you provide a [minimal example](http://stackoverflow.com/help/mcve) that actually runs? Also, are these old-style or new-style classes - which version of Python are you using? – jonrsharpe Jul 02 '14 at 22:18
  • The example I wrote runs on my python repl?? – frazman Jul 02 '14 at 22:21
  • That may be so, but the example you've *posted* is incorrectly indented, has comments where code should be and doesn't include any input data (what is `param` anyway?) – jonrsharpe Jul 02 '14 at 22:25
  • This helped me understand how the property decorator functions: http://stackoverflow.com/questions/17330160/python-how-does-the-property-decorator-work – Klaudikus Jul 03 '14 at 00:04

1 Answers1

1

You're misinterpreting what self._method(param) actually does when _method is a property. Since you are dealing with a property, the value currently stored in the property is retrieved.

tmp = self._method

And then that value is called as a function.

tmp(param)

Therefore there is no way to retrieve the value of param in the getter for _method since the interpreter hasn't even gotten to the point where param even matters.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358