473,796 Members | 2,911 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Testing for presence of arguments

Hi

I know how to set optional arguments in the function definition. Is there an
intrinsic function that determines if a certain argument was actually
passed ? Like the fortran 95 present() logical intrinsic ?

My required functionality depends on whether a certain argument is specified
at all. (Setting default values is *not* good enough.).

Thanks.
Aug 17 '05 #1
13 1585
On 8/17/05, Madhusudan Singh <sp************ **@spam.invalid > wrote:
I know how to set optional arguments in the function definition. Is therean
intrinsic function that determines if a certain argument was actually
passed ? Like the fortran 95 present() logical intrinsic ?

My required functionality depends on whether a certain argument is specified
at all. (Setting default values is *not* good enough.).


Could you just write the function as:

myFunc(*args, **kwargs):

....and then figure out what was passed?

--

# p.d.
Aug 17 '05 #2
On Wed, 17 Aug 2005 11:13:03 -0400,
Madhusudan Singh <sp************ **@spam.invalid > wrote:
I know how to set optional arguments in the function definition. Is
there an intrinsic function that determines if a certain argument was
actually passed ? Like the fortran 95 present() logical intrinsic ?
def f(**kw):
if kw.has_key('req uired_argument' ):
print "require_argume nt was present"
else:
print "require_argume nt was not present"
My required functionality depends on whether a certain argument is
specified at all. (Setting default values is *not* good enough.).


You can very nearly achieve this with carefully planned default
arguments. Put this into a module:

class _SemiPrivateCla ss:
pass

def f(required_argu ment=_SemiPriva teClass):
if required_argume nt == _SemiPrivateCla ss:
print "required_argum ent was probably not present"
else:
print "required_argum ent was present"

It's not impossible fool f, but an external module has to try very hard
to do so.

(All code untested.)

Regards,
Dan

--
Dan Sommers
<http://www.tombstoneze ro.net/dan/>
Aug 17 '05 #3
Madhusudan Singh wrote:
I know how to set optional arguments in the function definition. Is there an
intrinsic function that determines if a certain argument was actually
passed ? Like the fortran 95 present() logical intrinsic ?


People generally use a value that isn't a valid option, often None.

def my_func(a, b, c=None):
if c is None:
do something

If None is a valid value, make one that isn't:

unspecified = object()

def my_func(a, b, c=unspecified):
if c is unspecified:
do something
--
Benji York

Aug 17 '05 #4
I don't have the details ready - but in the ASPN cookbook are recipes
to e.g. figure insied a function f out how many results the caller of f
expects - and act accordingly. This boils down to inspect the
call-stack. So it ceratinly is possible.

However, I'd say it is almost 100% a design flaw. Or can you give us a
compelling reason why you need this behaviour?

Diez

Aug 17 '05 #5
Diez B. Roggisch wrote:
I don't have the details ready - but in the ASPN cookbook are recipes
to e.g. figure insied a function f out how many results the caller of f
expects - and act accordingly. This boils down to inspect the
call-stack. So it ceratinly is possible.

However, I'd say it is almost 100% a design flaw. Or can you give us a
compelling reason why you need this behaviour?

Diez


I am writing some code for a measurement application (would have used
fortran 95 if a library had been available for linux-gpib, but python is a
lot friendlier than C without the irritating and utterly pointless braces)
where one of the input parameters for the GPIB command is optional, and
depending on whether it is specified at all, an entire sequence of commands
has to be sent to the GPIB bus plus some input parameters recalculated.
Further, the sequence of commands depends on the range of values of the
optional parameter. And some of these commands in turn have similar
optional arguments.

All in all, the above would have been a bunch of simple one-liners with a
simple if block if python had something like the fortran 95 present()
intrinsic, but I could not find it. Hence my query. Just because there is
no simple and direct way of doing something in a language does not mean
that the application that requires it has a design flaw.

Unrelated question, how does one call a fortran 95 subroutine from python ?
I need really high speed of execution for that call (needed for each
measurement point, and is used to calculate some parameters for the
excitation for the next measurement point) and a scripting language would
not cut it.
Aug 17 '05 #6
Benji York wrote:
Madhusudan Singh wrote:
I know how to set optional arguments in the function definition. Is there
an intrinsic function that determines if a certain argument was actually
passed ? Like the fortran 95 present() logical intrinsic ?


People generally use a value that isn't a valid option, often None.

def my_func(a, b, c=None):
if c is None:
do something

If None is a valid value, make one that isn't:

unspecified = object()

def my_func(a, b, c=unspecified):
if c is unspecified:
do something
--
Benji York


Now, that was a very good suggestion. Thanks.
Aug 17 '05 #7
Dan Sommers wrote:
On Wed, 17 Aug 2005 11:13:03 -0400,
Madhusudan Singh <sp************ **@spam.invalid > wrote:
I know how to set optional arguments in the function definition. Is
there an intrinsic function that determines if a certain argument was
actually passed ? Like the fortran 95 present() logical intrinsic ?


def f(**kw):
if kw.has_key('req uired_argument' ):
print "require_argume nt was present"
else:
print "require_argume nt was not present"
My required functionality depends on whether a certain argument is
specified at all. (Setting default values is *not* good enough.).


You can very nearly achieve this with carefully planned default
arguments. Put this into a module:

class _SemiPrivateCla ss:
pass

def f(required_argu ment=_SemiPriva teClass):
if required_argume nt == _SemiPrivateCla ss:
print "required_argum ent was probably not present"
else:
print "required_argum ent was present"

It's not impossible fool f, but an external module has to try very hard
to do so.

(All code untested.)

Regards,
Dan


Thanks for the suggestion, but seems needlessly complicated for something
very simple.
Aug 17 '05 #8
Peter Decker wrote:
On 8/17/05, Madhusudan Singh <sp************ **@spam.invalid > wrote:
I know how to set optional arguments in the function definition. Is there
an intrinsic function that determines if a certain argument was actually
passed ? Like the fortran 95 present() logical intrinsic ?

My required functionality depends on whether a certain argument is
specified at all. (Setting default values is *not* good enough.).


Could you just write the function as:

myFunc(*args, **kwargs):

...and then figure out what was passed?


Seems a lot simpler than the other module suggestion, but another person has
posted a suggestion that is a lot more quick and elegant. Thanks anyways. I
might find this useful in some as yet unknown context.
Aug 17 '05 #9
> I am writing some code for a measurement application (would have used
fortran 95 if a library had been available for linux-gpib, but python is a
lot friendlier than C without the irritating and utterly pointless braces)
where one of the input parameters for the GPIB command is optional, and
depending on whether it is specified at all, an entire sequence of commands
has to be sent to the GPIB bus plus some input parameters recalculated.
Further, the sequence of commands depends on the range of values of the
optional parameter. And some of these commands in turn have similar
optional arguments.
I still don't see why default arguments like None won't do the trick.
If The argument _can_
be some value (let's say an int) or None, you still could go for a
default value like () or any other value
from a different domain.
All in all, the above would have been a bunch of simple one-liners with a
simple if block if python had something like the fortran 95 present()
intrinsic, but I could not find it. Hence my query. Just because there is
no simple and direct way of doing something in a language does not mean
that the application that requires it has a design flaw.
Certainly, but as certainly thinking in terms of one language while
using another is prone to
creating design flaws.

So far you still haven't convinced me that default arguments don't work
for you. To me it seems that
your idiom of present() is modeld by python's

if arg is None:
whatever

pretty mich. It might help if you show'd us what your code would like
_if_ python
had present() available. Then we can see what alternatives there are.
Unrelated question, how does one call a fortran 95 subroutine from python ?
I need really high speed of execution for that call (needed for each
measurement point, and is used to calculate some parameters for the
excitation for the next measurement point) and a scripting language would
not cut it.


Didn't ever try that, but either do it in C, or if fortran code can be
exposed as C lib, use that (ctypes is your friend). I'm not aware of a
fortran binding - but I never tried to find one. Basically Python can
interface with everything that can behave like C - which is the least
common denominator I think, so there should be some way.

Regards,

Diez

Aug 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
2675
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an option. Let's call the id of a class "cid" (for "class id"). The function signature should look like this: ******************************************
1
1744
by: TravelMan | last post by:
Is there any way to test for the presence of the Adobe Acrobat plug-in in Internet Explorer? It's doable in Netscape but so far I cannot get it to work in MSIE.
2
1386
by: spgmbl | last post by:
I have set up the local environment to use sqlserver mode testing. The article i followed to install was here: http://support.microsoft.com/default.aspx?scid=kb;en-us;317604 I also changed the value in the web.config file per article. However, i am not sure what to look for to help me verify and validate this is working correctly. I've sifted for articles on suggestions/ideas as to how this is working.. but have been unlucky
7
3464
by: Will McDonald | last post by:
Hi all. I'm writing a little script that operates on either stdin or a file specified on the command line when run. I'm trying to handle the situation where the script's run without any input gracefully but can't think how to test for stdin. I can test for a file argument on the command line using getopt and validate its existence with os.path.exists. If it doesn't I can print the useage.
3
7732
by: | last post by:
I'm using the DataList and GridView controls, and I am trying to wrap my head around the problem of conditionally showing or hiding cells/cell content based on the presence or absence of DB data. I am finding this sort of problem by far the most annoying part about working with ASP.NET controls. What I want is to know how to do three things: - conditionally show or hide an ImageField +column+ based on whether or not an the...
1
2079
by: David Wade | last post by:
Folks, Looking at the sanity checks for math.h/math.c in "The Standard C library" they use 4* DBL_EPSILON as the error range, which I think is equivalent to three bits of error. Is this appropriate on old style IBM 370 libraries with its nasty drifting prescion? If not what would be an appropriate value. Dave. P.S. Its just I am having problems with exp() and log() I expected these to
1
1080
by: rocco.rossi | last post by:
I'm employing xmlrpclib for a project at work, and I must say that I'm quite impressed with its effectiveness and ease of use. However, I was recently doing some tests when I realized that if the server was down, the client quite simply hanged (no use of "try ... except" here) with no error or tracebacks or exceptions whatsoever. Is there some way in Python of "testing" the presence of the server on the other end, so as to avoid this...
24
2530
by: David | last post by:
Hi list. What strategies do you use to ensure correctness of new code? Specifically, if you've just written 100 new lines of Python code, then: 1) How do you test the new code? 2) How do you ensure that the code will work correctly in the future? Short version:
11
2384
by: VK | last post by:
In the continuation of the discussion at "Making Site Opaque -- This Strategy Feasible?" and my comment at http://groups.google.com/group/comp.lang.javascript/msg/b515a4408680e8e2 I have realized that despite suggestions to use DHTML-based modal dialogs are very common? there is not a single fully functional reliable copyright-free cross-browser alternative to say MsgBox (VBScript) or showModalDialog (IE). This way such suggestions up to...
1
10201
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
10021
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...
1
7558
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
6802
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
5454
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4130
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
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.