ignoring keywords on func. call

  • Thread starter Brano Zarnovican
  • Start date
B

Brano Zarnovican

Hi !

If I define 'f' like this

def f(a):
print a

then, the call with keywords

f(1, optional=2)

fails. I have to change 'f' to

def f(a, **kwrds):
print a

to ignore optional parameters.

BUT..

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

I would like to avoid code like this:
k = {}
k['optional'] = 2
try:
f(1, **k)
except TypeError:
f(1)

Also, the problem is that I don't know if the TypeError
was caused by calling 'f' with keywords or somewhere
"inside" f.

You can also say that I need to specify optional parameters
on caller side (not called side).

BranoZ
 
F

Fredrik Lundh

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

I would like to avoid code like this:
k = {}
k['optional'] = 2
try:
f(1, **k)
except TypeError:
f(1)

why would you write code like this? what's the use case?
Also, the problem is that I don't know if the TypeError
was caused by calling 'f' with keywords or somewhere
"inside" f.

You can also say that I need to specify optional parameters
on caller side (not called side).

you can use reflection mechanisms to check the target function
signature, but I won't tell you how to do that until you show me
a reasonable use case ;-)

</F>
 
B

Brano Zarnovican

Q: Can you call 'f' with keywords that will be

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
 
S

Steve Holden

Brano said:


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
[...]

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)
Then they haven't programmed to your API, and deserve to be rewarded
with an error message - it's the only way they can be informed they
aren't writing to your standard, surely?

regards
Steve
 
P

Peter Otten

Brano said:
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)

I deem it more likely that that same somebody will not overlook it and dump
your Tree class altogether for not adhering to a sensible convention. Why
don't you just introduce a getitem_ex(self, key, all the options you like)
method and define __getitem__() in terms of that method? E. g.

def getitem_ex(self, key, some_option=None):
# ...

def __getitem__(self, key):
return self.getitem_ex(key, some_option=True)

Your docs could then recommend to override getitem_ex() instead of
__getitem__() and you and somebody would live happily ever after (*).

Peter

(*) or at least without calling each other names right from the start.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,234
Messages
2,571,178
Members
47,811
Latest member
Adisty

Latest Threads

Top