Custom placeholder like None in python

后端 未结 3 633
庸人自扰
庸人自扰 2020-12-19 04:33

I\'m using argspec in a function that takes another function or method as the argument, and returns a tuple like this:

((\"arg1\", obj1), (\"arg2\", obj2), .         


        
相关标签:
3条回答
  • 2020-12-19 05:03

    There isn't any reason to not use such sentinel objects for such purposes. As an alternative to a class object, you could also create a singleton instance of a dynamically created type:

    NoDefault = type('NoDefault', (object,), {
        '__str__': lambda s: 'NoDefault', '__repr__': lambda s: 'NoDefault'})()
    
    0 讨论(0)
  • 2020-12-19 05:07

    My favourite sentinel is Ellipsis, and if I may quote myself:

    it's there, it's an object, it's a singleton, and its name means "lack of", and it's not the overused None (which could be put in a queue as part of normal data flow). YMMV.

    0 讨论(0)
  • 2020-12-19 05:23

    I had a similar situation some time ago. Here's what I came up with.

    # 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,), {})
    

    It can also act as a null callable and sequence.

    0 讨论(0)
提交回复
热议问题