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

ignoring keywords on func. call

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

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Brano Zarnovican wrote:
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>

Jul 18 '05 #2

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

Jul 18 '05 #3

P: n/a
Brano Zarnovican wrote:
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

[...]
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
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/

Jul 18 '05 #4

P: n/a
Brano Zarnovican wrote:
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.
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.