Testing for presence of arguments
Dan Sommers
me at privacy.net
Wed Aug 17 11:41:26 EDT 2005
On Wed, 17 Aug 2005 11:13:03 -0400,
Madhusudan Singh <spammers-go-here at 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('required_argument'):
print "require_argument was present"
else:
print "require_argument 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 _SemiPrivateClass:
pass
def f(required_argument=_SemiPrivateClass):
if required_argument == _SemiPrivateClass:
print "required_argument was probably not present"
else:
print "required_argument 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.tombstonezero.net/dan/>
More information about the Python-list
mailing list