Skip to content Skip to sidebar Skip to footer

Function To Make Functions Callable Doesn't Work Properly

I'm having the problem of a button executing its' command when it's created. To stop this I have got a function, which can stop this behavior This is the function which makes funct

Solution 1:

I believe that's what you're looking for:

class Callable(object):
    def __init__(self, func, *args, **kwds):
        self.func = func
        self.args = args
        self.kwds = kwds

    def __call__(self, *args, **kwds):
         return self.func(*self.args, *args, **self.kwds, **kwds)

    def __str__(self):
        return self.func.__name

You need to unpack it with the * operator, and ** for keyword arguments. That way you pass your variables and the function's call variables.

UPDATE:

For python versions older than 3.5, this will work:

class Callable(object):
    def __init__(self, func, *args, **kwds):
        self.func = func
        self.args = args
        self.kwds = kwds

    def __call__(self, *args, **kwds):
        args = self.args + args
        kwargs = kwds.copy()
        kwargs.update(self.kwds)
        return self.func(*args, **kwargs)

    def __str__(self):
        return self.func.__name

Using this solution, you will first give the variables acquired by the __init__ then the variables passed to the __call__.


Solution 2:

Your class is used to provide additional context to tkinter callbacks so the callback can actually do something useful. You almost have it right except that you need to unpack the original args and kwds when calling the function. Also, don't include any args in __call__ because you don't accept any.

class Callable(object):
    def __init__(self, func, *args, **kwds):
        self.func = func
        self.args = args
        self.kwds = kwds

    def __call__(self):
         return self.func(*self.args, **self.kwargs)

    def __str__(self):
        return self.func.__name


def some_callback(a, b, c=None):
    print(a,b,c)

Button(text="Do not press", 
    command=Callable(some_callback, 1, 2, 3))

This can also be done without a class, using lambdas instead

def some_callback(a, b, c=None):
    print(a,b,c)

Button(text="Do not press", 
    command=lambda: some_callback(1, 2, 3))

Solution 3:

Please consider using the following class. It allows you to specify positional arguments and keyword arguments at the time of either instance creation or instance invocation (creating a new instance or calling it). Since it is ambiguous what would be meant if either types of arguments were specified at both times, the class refuses to guess what order or priorities were intended and raises a RuntimeError to prevent undefined behavior. Also, the __str__ method should still work if your function or other callable object does not have a __name__ attribute.

class Callable:

    def __init__(self, function, *args, **kwargs):
        self.__function, self.__args, self.__kwargs = function, args, kwargs

    def __call__(self, *args, **kwargs):
        if (args or kwargs) and (self.__args or self.__kwargs):
            raise RuntimeError('multiple args and kwargs are not supported')
        if args or kwargs:
            return self.__function(*args, **kwargs)
        return self.__function(*self.__args, **self.__kwargs)

    def __str__(self):
        return getattr(self.__function, '__name__', 'function')

You might also want to take a look at functools.partial for a well-defined object with very similar behavior. You may be able to avoid defining your own Callable class and use the functools module instead. That way, there is less code that you have to manage in you project.


Post a Comment for "Function To Make Functions Callable Doesn't Work Properly"