Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > function argument dependent on another function argument?

Reply
Thread Tools

function argument dependent on another function argument?

 
 
Aaron Brady
Guest
Posts: n/a
 
      01-19-2009
On Jan 18, 12:02*pm, Rob Williscroft <(E-Mail Removed)> wrote:
> Aaron Brady wrote innews:582ef883-0176-4984-9521-6c1894636891@a26g2000prf..googlegroups.com
> in comp.lang.python:
>
>
>
> > On Jan 18, 10:44*am, Rob Williscroft <(E-Mail Removed)> wrote:
> >> Aaron Brady wrote
> >> innews:6a10378f-addb-4d56-bc1b-0c382b3cb957@t26g2000prh

> > .googlegroups.com
> >> in comp.lang.python:

>
> >> > It is too bad that it is so much work to detect whether 'y' was
> >> > passed in the function call directly. *However, sentinel is just as
> >> > good (or nearly); at worst, you need one sentinel per argument per
> >> > function,

>
> >> One per Module should be good enough. The only reason None doesen't
> >> suffice is that it has other legitimate uses. *Though to be honest
> >> I would always use None as the sentinel if it wasn't a legitimate
> >> argument.

>
> >> > which is possible to create, which has a specific meaning. *If you
> >> > are
> >> > making systematic function calls, e.g. with a dictionary or list,
> >> > you can just use the sentinel in the dictionary.

>
> >> IIUYC then, one sentinel is still only needed as the missing argument
> >> is indicated by *both* position and value or by name and value (in
> >> the case of a keyword-dictionary), so seperate distinct sentinel
> >> objects aren't required, for example:

>
> >> SENTINEL = object()

>
> >> def f( a, b, c = SENTINEL, d = SENTINEL ):
> >> * print( "values: %r" % ( ( a, b, c, d ), ) )
> >> * if c is SENTINEL:
> >> * * print( "c is missing" )
> >> * if d is SENTINEL:
> >> * * print( "d is missing" )

>
> >> f( *( 1, 2, SENTINEL, SENTINEL ) )

>
> >> f( **dict( a = 1 , b = 2, d = 4 ) )

>
> >> f( **dict( a = 1 , b = 2, d = 4, c = SENTINEL ) )

> > I don't have a concrete example, so you may prove to be right, but I'm
> > not convinced.

>
> I'm afraid I can't think of a use case for passing default values around
> eiither, and I suspect if we were to come up with one, a better solution
> that didn't involve passing default values around could be found.
>
> > If you have one function with an argument that defaults to an empty
> > list, and calls another with an argument that defaults to an empty
> > dict, then what is the meaning of passing sentinel to the first one?
> > Whereas, if each had their own, then passing the first one's default
> > would mean the empty list, and passing the second one's default would
> > mean the dict.

>
> If you *mean* to pass an "empty list" or "empty dict"'s you should do
> it like:
>
> * function_taking_list( [] )
> * function_taking_dict( {} )
>
> Its when you don't (have reason to) care that you need default arguments.


'None' isn't a valid value for many standard library functions. So,
if you try to pass it meaning, "Whatever the default value you usually
use is," you'll get an error. Sometimes, the functions don't even use
a public sentinel, so if you want the default value, you either have
to know what it is, or you can't pass anything to that parameter.
This is usually possible, it just prevents making a uniform call to a
function. If you need to do some calculations to determine what
parameters you're going to pass, you're stuck testing their presence
with 'if-else' combinations, then calling individually.
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-19-2009
On Sun, 18 Jan 2009 22:28:04 +0000, Steven D'Aprano wrote:

> Built-ins rarely accept None as a sentinel, slice() being a conspicuous
> exception. This is sometimes a nuisance when writing wrappers:
>
> def my_find(S, sub, start=None, end=None):
> """Like string.find() only with pre-processing.""" pre_process() #
> stub for something complicated if end is None and start is None:
> return S.find(sub)
> elif end if None:
> return S.find(sub, start)
> else:
> return S.find(sub, start, end)


Typical.

As of Python 2.6, string.find accepts None as sentinels. All my
beautiful, beautiful code made obsolete!!! *wink*


--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable argument function as a parameter of a variable argument function AikidoGuy C Programming 11 11-21-2011 10:43 PM
non-dependent vs. dependent template names puzzlecracker C++ 1 08-07-2008 07:42 AM
Argument-Dependent Lookup siddhu C++ 1 05-14-2007 10:57 PM
argument dependent lookup Martin Vorbrodt C++ 13 10-25-2005 10:21 PM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM



Advertisments