473,461 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ignoring keywords on func. call

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
4 1336
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
> > 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

90
by: Mark Hahn | last post by:
"Michael Geary" <Mike@Geary.com> wrote ... >Does anyone have some sample code where obj$func() would be used? > (Apologies if I missed it.) There have been so many messages about delegation...
31
by: Brian Sabbey | last post by:
Here is a pre-PEP for what I call "suite-based keyword arguments". The mechanism described here is intended to act as a complement to thunks. Please let me know what you think. Suite-Based...
3
by: Jason | last post by:
Hi, Im running windows xp pro and compiling using dev c++ 4. I have the following situation: #include <iostream> #include <string> using namespace std; int main() {
5
by: modemer | last post by:
I saw someone use the following code: void func(MyClass *& myCls) { myCls->test(); } // call func(): func(new MyClass);
14
by: Jason Heyes | last post by:
I want to write a class that supports operations on keywords belonging to the C++ programming language. The following program repeatedly prompts the user for a keyword until 'explicit' is finally...
10
by: Jon | last post by:
I'm investiging multi-threaded GUI applications; specifically the technique used to ensure that controls are only modified under the correct thread. The standard technique is to use ...
9
by: Nenad Loncarevic | last post by:
I am a geologist, and over the years I've accumulated quite a number of proffesional papers on the subject, in various publications. I would like to make a database that would help me find the...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
28
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: void func (void) { } void func2 (void) {
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.