By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Overloading __getitem__

P: n/a
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 foo['a']
print foo['a', crazy = True]
Is it somehow possible to overload __getitem__ with an additional
argument? Are there other possibilities to achiev this? Or is
the only solution to this to write a normal function call
`def my_get (self, key, crazy=False)'?
Ciao
Andreas
Jun 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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 wanted.
or
crazy = True
print foo['a',crazy]


"Andreas Matthias" <am**@kabsi.atwrote in message
news:uf************@buckbeak.hogwarts...
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 foo['a']
print foo['a', crazy = True]
Is it somehow possible to overload __getitem__ with an additional
argument? Are there other possibilities to achiev this? Or is
the only solution to this to write a normal function call
`def my_get (self, key, crazy=False)'?
Ciao
Andreas

Jun 27 '08 #2

P: n/a

"inhahe" <in****@gmail.comwrote in message
news:Ly*******************@bignews3.bellsouth.net. ..
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, i don't know.)


Jun 27 '08 #3

P: n/a
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)

and then
print foo[1,2] #not crazy
print foo[1,2,crazy] #crazy

I *think* that would work


"Andreas Matthias" <am**@kabsi.atwrote in message
news:uf************@buckbeak.hogwarts...
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 foo['a']
print foo['a', crazy = True]
Is it somehow possible to overload __getitem__ with an additional
argument? Are there other possibilities to achiev this? Or is
the only solution to this to write a normal function call
`def my_get (self, key, crazy=False)'?
Ciao
Andreas

Jun 27 '08 #4

P: n/a
in****@gmail.com 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:

def __getitem__(*args):
Apparently, args already is a tuple, so this should be:

def __getitem__(self, args):

Is this documented somewhere? I couldn't find it anywhere.

Thanks.
Ciao
Andreas
Jun 27 '08 #5

P: n/a
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 equivalent to
d[(1,2)] so I guess it makes sense that args is a tuple.

Jun 27 '08 #6

P: n/a
En Thu, 22 May 2008 20:38:39 -0300, Andreas Matthias <am**@kabsi.atescribió:
in****@gmail.com 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:

def __getitem__(*args):

Apparently, args already is a tuple, so this should be:

def __getitem__(self, args):

Is this documented somewhere? I couldn't find it anywhere.
No, that's not correct. First, there is nothing special with the arguments to dict.__getitem__ -- except that the syntax obj[index] provides a delimiter and it allows for obj[a,b] as a shortcut for obj[(a,b)] -obj.__getitem__((a,b))

You may use the *args notation in any function; it is always a tuple (a singleton, when you call the function with only one argument)

pydef foo(*args): print args
....
pyfoo(1)
(1,)
pyfoo(1,2)
(1, 2)
pyfoo((1,2))
((1, 2),)

Compare with:

pydef bar(arg): print arg
....
pybar(1)
1
pybar(1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bar() takes exactly 1 argument (2 given)
pybar((1,2))
(1, 2)

--
Gabriel Genellina

Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.