473,797 Members | 3,183 Online
Bytes | Software Development & Data Engineering Community
+ 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 1357
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__(sel f, 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__(sel f, 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=Non e):
# ...

def __getitem__(sel f, key):
return self.getitem_ex (key, some_option=Tru e)

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
4003
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 and binding since Greg originally posted his meowing cat message that it's hard to remember what the original problem was that Greg pointed out. At that time Prothon had no solution for the problem. Now there is a released solution but it is...
31
2445
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 Keyword Arguments ----------------------------- Passing complicated arguments to functions is currently awkward in Python. For example, the typical way to define a class property winds up polluting the class's namespace with the property's get/set...
3
43299
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
1629
by: modemer | last post by:
I saw someone use the following code: void func(MyClass *& myCls) { myCls->test(); } // call func(): func(new MyClass);
14
2363
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 entered: #include <iostream> #include "KeyWord.h" int main() { KeyWord word;
10
3281
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 if(InvokeRequired == true) ... BeginInvoke(...) ... I have some code that ensures that an event fired from a worker (non-GUI) thread will use the appropriate BeginInvoke call for synchronisation. By offloading the synchronisation onto the worker thread the...
9
3384
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 information I want, based on keywords mentioned in the needed paper. Since I don't feel like inventing hot water, I thought I'd ask you people a few questions. I was planning on making tables with a many-to-many relationship for papers and...
37
3993
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 a function
28
3698
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: void func (void) { } void func2 (void) {
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10469
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10209
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9066
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7560
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6803
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.