En Thu, 22 May 2008 20:38:39 -0300, Andreas Matthias <[EMAIL PROTECTED]>
escribió:
> [EMAIL PROTECTED] wrote:
>
>> actually i ddin't think about the fact that you're overloading dict, which
>> can already take multiple values in getitem
>
> Oh, I didn't know that. I totally misinterpreted the err
> Apparently, args already is a tuple, so this should be:
>
> def __getitem__(self, args):
>
> Is this documented somewhere? I couldn't find it anywhere.
>
Don't know, I just assumed it would take multiple arguments because I knew I
had seen the form d[1,2] before, which incidentally is equival
[EMAIL PROTECTED] wrote:
> actually i ddin't think about the fact that you're overloading dict, which
> can already take multiple values in getitem
Oh, I didn't know that. I totally misinterpreted the error message.
> so how about
>
> class crazy: pass
>
> and then in your dict class:
>
> d
actually i ddin't think about the fact that you're overloading dict, which
can already take multiple values in getitem
so how about
class crazy: pass
and then in your dict class:
def __getitem__(*args):
if args[-1] is crazy:
return self.get(args[:-1])*5
else:
return self.get(args)
"inhahe" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> crazy = True
> print foo['a',crazy]
>
just to clarify, you could use it like:
crazy = "I'm crazy" #this only has to be done once
print foo['a'] #not crazy
print foo['a',crazy] #crazy
(this may be totally unPythonic
it seems like you can't do it exactly the way you're trying but you could do
this
def __getitem__(*args):
if len(args) > 1 and args[1]: return self.get(args[0]) * 5
return self.get(args[0])
then you would use it like
print foo['a']
print foo['a',True]
or even
print foo['a',"crazy"]
if you
The following code doesn't run but I hope you get what I
am trying to do.
class my_dict (dict):
def __getitem__ (self, key, crazy = False):
if crazy == True:
return 5 * self.get(key)
else:
return self.get(key)
foo = my_dict()
foo['a'] = 123
print fo