I have the following code (shortened to leave just the essentials):
class Strange():
def setter(self, val):
self._val = val
val = property(lambda: self._val, setter)
PyDev in Eclipse Helios barks that self is not visible, which I think should be true and agrees with the explicit philosophy of Python. However the code is running fine in the ipython 2.7 and is used in the codebase.
Is this a security hole, a lexical scoping quirk? Or is there a PEP or documentation explaining this and it's just PyDev missing something?
EDIT: Answering the comments: It works on my machine, maybe your version of Python and OS differs. But thanks, this somehow answers my question about it not being the standard behaviour.
Here is the console output in my case:
barszcz:~ $ uname -a
Linux barszcz 3.4.9-1-ARCH #1 SMP PREEMPT Wed Aug 15 18:59:31 CEST 2012 x86_64 GNU/Linux
barszcz:~ $ ipython2
Python 2.7.3 (default, Apr 24 2012, 00:00:54)
Type "copyright", "credits" or "license" for more information.
IPython 0.13 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:class Strange():
: def setter(self, val):
: self._val = val
:
: val = property(lambda: self._val, setter)
:
:<EOF>
In [2]: Strange
Out[2]: __main__.Strange
In [3]: Strange()
Out[3]: <__main__.Strange instance at 0x26425f0>
EDIT2:
Ok, so I get an error on calling Strange().val
, just as Martijn suggested. But the strange thing it is a runtime error not a bytecode compilation error:
In [4]: Strange().val
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-88f3db8b81d0> in <module>()
----> 1 Strange().val
TypeError: <lambda>() takes no arguments (1 given)
I would expect the content of the lambda to be run on executing Strange
definition already... If somebody gets the error on definition already, please let me know in the comments.