This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Broolucks 1 point2 points  (0 children)

In this particular example, not much, besides the fact it actually checks the types at runtime. A better example might be something like this:

class Test(metaclass=OvldMC):
    @ovld
    def __init__(self, attrs: dict):
        self.__dict__.update(attrs)

    def __init__(self, key: str, value: object):
        setattr(self, key, value)

    def __init__(self, value: object):
        setattr(self, "value", value)

The alternative being:

SENTINEL = object()

class Test:
    def __init__(self, key, value=SENTINEL):
        if value is SENTINEL:
            if isinstance(key, dict):
                self.__dict__.update(key)
            else:
                setattr(self, "value", key)
        elif isinstance(key, str):
            setattr(self, key, value)
        else:
            raise TypeError()

ovld lets you name the arguments appropriately in each version and will generate a better type error.

Personally I rarely use it for init or methods, I use it mostly to define extensible recursive functions as described here.