Trouble when overloading operator[ ]
david_ullrich at my-deja.com
david_ullrich at my-deja.com
Sat Jul 22 13:24:24 EDT 2000
In article <6%_d5.1964$6E.417529 at ptah.visi.com>,
ge at nowhere.none (Grant Edwards) wrote:
> In article <8l9qfp$gc6$1 at nnrp1.deja.com>, david_ullrich at my-deja.com
wrote:
>
> >> You're using a single index whose value is the expression 2,3.
> >> The expression 2,3 evaluates to a tuple, which is passed to
> >> _getitem__, just as would the value of any other expression
> >> used as an index.
> >
> > Ok, then I should revise what I said: You can use the
> >notation [2,3] to accomplish the same thing.
> >
> > No doubt you're exactly right. But evidently there's a
> >difference between [] indices and function parameters here:
> >
> >def hmm(x):
> > return x
> >
> >print hmm(2,3)
> >
> >gives an error (hence my suspicion that someone might
> >expect whatever[2,3] to give an error without trying it...)
> >
> > Why is "2,3" a single expression in whatever[2,3]
> >but not in hmm(2,3)?
>
> Because that's the way the language syntax is defined. :)
Yeah, actually I realized that the answer was "because
that's the way it is", I guess what I was really trying to
suggest that since function parameters don't work that
way it's not so obvious that __getitem__ indices do.
> The syntax for a function call includes the commas separating
> the parameters. The syntax for an index doesn't include any
> commas, so the commas are part of an expression yeilding a
> tuple. It would be a bit more obvious if one used foo[(4,5)].
Of course. But the fact that we can write foo[4,5] instead
is very convenient (I was writing foo[(4,5)] in a certain
context once, wishing there was a syntax more like the
syntax for "2-d arrays" elsewhere. Forgot the parentheses
once - that solved that problem.)
DU
Sent via Deja.com http://www.deja.com/
Before you buy.
More information about the Python-list
mailing list