ignoring keywords on func. call

Brano Zarnovican zarnovican at pobox.sk
Wed Apr 6 08:39:36 EDT 2005


> > Q: Can you call 'f' with keywords that will be
> >  ignored, without changing 'f's definition ?
>
> no.

OK. Thank you for an answer.

> what's the use case?

I have extended the dict class, so that my __getitem__ accepts an
optional parameter

class MyTree(dict):

  def __getitem__(self, key, **k):
    ..

  def lookup(self, seq):
    node = self
    for k in seq:
      node = node.__getitem__(k, someoption=True)

lookup takes a sequence of keys to lookup in hierarchy
of Tree nodes. But I would like lookup to work also on objects
that are not subclasses of Tree. It has to work on any dict-like class.

If a user defines a custom class, he will intuitively define
__getitem__
like this:

def MyNode(??):
  def __getitem__(self, key):
    ..

I could check in 'lookup'

  if isinstance(node, Tree):
      node = node.__getitem__(k, someoption=True)
  else:
      node = node.__getitem__(k)

But it still doesn't guarantee that __getitem__ accepts keywords.
(What if somebody will extend the Tree class and overlook the
definition of __getitem__ and define a "classic" one)

BranoZ




More information about the Python-list mailing list