我在一个函数中使用argspec,该函数将另一个函数或方法作为参数,并返回一个这样的元组:
(("arg1", obj1), ("arg2", obj2), ...)
Run Code Online (Sandbox Code Playgroud)
这意味着传递函数的第一个参数是arg1,它的默认值为obj1,依此类推.
这是一个问题:如果它没有默认值,我需要一个占位符值来表示这一点.我不能使用None,因为那时我无法区分无默认值和默认值是None.对于False,0,-1等也是如此.我可以使它成为一个单元素的元组,但是然后用于检查它的代码将是丑陋的,我不能轻易将它变成一个字典.所以我想我会创建一个非类似的非对象,这就是我想出来的:
class MetaNoDefault(type):
def __repr__(cls):
return cls.__name__
__str__ = __repr__
class NoDefault(object):
__metaclass__ = MetaNoDefault
Run Code Online (Sandbox Code Playgroud)
现在("arg1", NoDefault)表明arg1没有默认值,我可以做类似的事情if obj1 is NoDefault:.元类使它打印为NoDefault而不是<class '__main__.NoDefault'>.
有没有理由不这样做?有更好的解决方案吗?
前段时间我也遇到过类似的情况。这是我想出的。
# Works like None, but is also a no-op callable and empty iterable.
class NULLType(type):
def __new__(cls, name, bases, dct):
return type.__new__(cls, name, bases, dct)
def __init__(cls, name, bases, dct):
super(NULLType, cls).__init__(name, bases, dct)
def __str__(self):
return ""
def __repr__(self):
return "NULL"
def __nonzero__(self):
return False
def __len__(self):
return 0
def __call__(self, *args, **kwargs):
return None
def __contains__(self, item):
return False
def __iter__(self):
return self
def next(*args):
raise StopIteration
NULL = NULLType("NULL", (type,), {})
Run Code Online (Sandbox Code Playgroud)
它还可以充当空可调用和序列。
| 归档时间: |
|
| 查看次数: |
1435 次 |
| 最近记录: |