测试参数的存在 [英] Testing for presence of arguments

查看:42
本文介绍了测试参数的存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我知道如何在函数定义中设置可选参数。是否有一个

内在函数来确定某个参数是否实际上是通过了?b $ b?就像fortran 95 present()逻辑内在一样?

我所需的功能取决于是否指定某个参数

。 (设置默认值是*不够*。)。


谢谢。

解决方案

On 8 / 17/05,Madhusudan Singh< sp ************** @ spam.invalid>写道:

我知道如何在函数定义中设置可选参数。是否存在内在函数来确定某个参数是否真的被传递了?就像fortran 95 present()逻辑内在一样?

我所需要的功能取决于是否指定某个参数
。 (设置默认值是*不够*。)。




你能不能把这个函数写成:


myFunc (* args,** kwargs):


....然后弄清楚通过了什么?


-


#pd


2005年8月17日星期三11:13:03 -0400,

Madhusudan Singh < SP ************** @ spam.invalid>写道:

我知道如何在函数定义中设置可选参数。是否有一个内在函数来确定某个参数是否实际通过了?像fortran 95 present()逻辑内在?


def f(** kw):

如果kw.has_key(''required_argument''):

print" ; require_argument存在

else:

print" require_argument not present"

我所需的功能取决于某个参数是否为
指定。 (设置默认值是*不够*。)。




您可以通过精心策划的默认

参数来实现这一目标。把它放到一个模块中:


class _SemiPrivateClass:

pass


def f(required_argument = _SemiPrivateClass):

if required_argument == _SemiPrivateClass:

print" required_argument可能不存在"

else:

打印required_argument存在


这不是不可能的傻瓜,但外部模块必须非常努力才能这样做。
这样做。 />

(所有代码未经测试。)


问候,

Dan


-

Dan Sommers

< http://www.tombstonezero.net/dan/>


< blockquote> Madhusudan Singh写道:

我知道如何在函数定义中设置可选参数。是否有一个内在函数来确定某个参数是否真的被传递了?就像fortran 95 present()逻辑内在?




人们通常使用一个不是有效选项的值,通常是None。


def my_func(a,b,c =无):

如果c为无:

做点什么


如果None是有效值,请创建一个不是:


unspecified = object()

def my_func(a, b,c =未指定):

如果c未指定:

做点什么

-

Benji York


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.

解决方案

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.


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(''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/>


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


这篇关于测试参数的存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆