Make obj[] valid syntax?
I'm not sure if this is a good idea or not, but - hey - this is python.ideas ;-) The following statements currently raise a SyntaxError: obj[] = something x = obj[] I propose to make these statements valid syntax. 'obj[]' should behave like 'obj[()]' does: Call __getitem__ or __setitem__ with an empty tuple. My use case is in a COM library (comtypes). Some COM properties require one or more arguments; this is not a problem since one could write obj.prop[1, 2, 3] Sometimes, however, arguments are optional. Unfortunately one has to write obj.prop[()] to pass an empty tuple to __getitem__ or __setitem__, which looks strange imo. Comments? Thomas
On Wed, Nov 14, 2007 at 07:29:52PM +0100, Thomas Heller wrote:
'obj[]' should behave like 'obj[()]' does:
I remember it was discussed and rejected a year or two ago. Still -1 from me. Explicit [()] is better than implicit []. Oleg. -- Oleg Broytmann http://phd.pp.ru/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.
Oleg Broytmann schrieb:
On Wed, Nov 14, 2007 at 07:29:52PM +0100, Thomas Heller wrote:
'obj[]' should behave like 'obj[()]' does:
I remember it was discussed and rejected a year or two ago. Still -1 from me. Explicit [()] is better than implicit [].
However: obj[(1, 2, 3)] is the same as obj[1, 2, 3]
On Nov 14, 2007 10:38 AM, Thomas Heller <theller@ctypes.org> wrote:
Oleg Broytmann schrieb:
On Wed, Nov 14, 2007 at 07:29:52PM +0100, Thomas Heller wrote:
'obj[]' should behave like 'obj[()]' does:
I remember it was discussed and rejected a year or two ago. Still -1 from me. Explicit [()] is better than implicit [].
However: obj[(1, 2, 3)] is the same as obj[1, 2, 3]
So what? x = is not equivalent to x = () -- --Guido van Rossum (home page: http://www.python.org/~guido/)
Guido van Rossum schrieb:
So what?
x =
is not equivalent to
x = ()
I won't argue this with you ;-)
Oleg Broytmann schrieb:
On Wed, Nov 14, 2007 at 07:29:52PM +0100, Thomas Heller wrote:
> 'obj[]' should behave like 'obj[()]' does:
I remember it was discussed and rejected a year or two ago. Still -1 from me. Explicit [()] is better than implicit [].
However: obj[(1, 2, 3)] is the same as obj[1, 2, 3]
1, 2, 3 is a tuple, and () is a tuple, should there be a syntax for an empty tuple without parenthesis?
Thomas, there were many arguments in the previous discussion. This one was there, too. But finally the proposal was rejected.
I see that my proposal probably won't fly. This encourages me to describe my full wish, just for fun: It would be nice if I could have positional AND keyword arguments for __getitem__ and __setitem__, so that I could write code like this (COM has named parameters also): x = obj.prop[1, 2, lcid=0] x = obj.prop[] obj.prop[1, 2, lcid=0] = "foo" obj.prop[] = "foo" or even x = obj.prop[1, 2, lcid=0] x = obj.prop[] x = obj.prop # same as previous line (now how would THAT work?) obj.prop[1, 2, lcid=0] = "foo" obj.prop[] = "foo" obj.prop = "foo" # same as previous line I retract my proposal. VB-ly, yours Thomas
On Wed, Nov 14, 2007 at 07:38:34PM +0100, Thomas Heller wrote:
Oleg Broytmann schrieb:
On Wed, Nov 14, 2007 at 07:29:52PM +0100, Thomas Heller wrote:
'obj[]' should behave like 'obj[()]' does:
I remember it was discussed and rejected a year or two ago. Still -1 from me. Explicit [()] is better than implicit [].
However: obj[(1, 2, 3)] is the same as obj[1, 2, 3]
1, 2, 3 is a tuple, and () is a tuple, should there be a syntax for an empty tuple without parenthesis? Thomas, there were many arguments in the previous discussion. This one was there, too. But finally the proposal was rejected. Oleg. -- Oleg Broytmann http://phd.pp.ru/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.
"Oleg Broytmann" <phd@phd.pp.ru> wrote in message news:20071114190057.GA32728@phd.pp.ru... | 1, 2, 3 is a tuple, and () is a tuple, should there be a syntax for an | empty tuple without parenthesis? The only thing I have thought of is a bare comma, but I like that even less than the () exception and expect most would agree ;-)
Why can't you use call syntax, i.e. obj.prop(1, 2, 3)? On Nov 14, 2007 10:29 AM, Thomas Heller <theller@ctypes.org> wrote:
I'm not sure if this is a good idea or not, but - hey - this is python.ideas ;-)
The following statements currently raise a SyntaxError:
obj[] = something x = obj[]
I propose to make these statements valid syntax. 'obj[]' should behave like 'obj[()]' does: Call __getitem__ or __setitem__ with an empty tuple.
My use case is in a COM library (comtypes).
Some COM properties require one or more arguments; this is not a problem since one could write obj.prop[1, 2, 3]
Sometimes, however, arguments are optional. Unfortunately one has to write obj.prop[()] to pass an empty tuple to __getitem__ or __setitem__, which looks strange imo.
Comments?
Thomas
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
On Nov 14, 2007 10:37 AM, Thomas Heller <theller@ctypes.org> wrote:
Guido van Rossum schrieb:
Why can't you use call syntax, i.e. obj.prop(1, 2, 3)?
Because I cannot set the property in this way:
obj.prop(1, 2, 3) = "foo"
Of course I know that obj.set_prop(1, 2, 3, "foo") would work.
And can't you arrange for obj.prop = "foo" to work as well as obj.prop[1,2,3] = "foo"? -- --Guido van Rossum (home page: http://www.python.org/~guido/)
Guido van Rossum schrieb:
On Nov 14, 2007 10:37 AM, Thomas Heller <theller@ctypes.org> wrote:
Guido van Rossum schrieb:
Why can't you use call syntax, i.e. obj.prop(1, 2, 3)?
Because I cannot set the property in this way:
obj.prop(1, 2, 3) = "foo"
Of course I know that obj.set_prop(1, 2, 3, "foo") would work.
And can't you arrange for obj.prop = "foo" to work as well as obj.prop[1,2,3] = "foo"?
Sure, but this requires to use [] for setting and () for getting the property.
participants (4)
-
Guido van Rossum
-
Oleg Broytmann
-
Terry Reedy
-
Thomas Heller