Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making a class callable in same instance

class Foo(object):
    def tick(self):
        print("something")

class Bar(object):
    def __init__(self):
        self.foo = Foo()

    def tick(self):
        #Here's what I do....
        self.foo.tick()

        #here's what my goal would be
        self.foo()

b = Bar()
b.tick()

That's essentially my goal. From what I've gathered I could change the tick function to __call__ and that would allow me to do what I wanted. A couple of the other answers said that this would make a new instance of the object, does that mean that it would use self.foo's memory? or would it make a whole new object, newly instanced? or make a copy of self.foo?

Also a couple of drawbacks to this which may or may not manifest themselves come to mind. For a particular part of my program, I check to see if the object has a __call__ to determine if the argument I'm passing is a function or a variable, and I don't really think I would want to allow that to be called (even though, I suppose the class technically would be a function at that point.) Is there any way to distinguish between a function and a callable class?

Is there anything else that would make doing this undesirable (and is it a pythonic way to work?)? My next thought had been that given that other variable prefixed with __ cant be used outside their class, but that doesnt seem to be the case here.

like image 995
DanielCardin Avatar asked Jan 29 '13 15:01

DanielCardin


People also ask

How to make a Python class instance callable?

In python, we can define a __call__ function in class to make a class instance callable. In order to understand args and kwargs, you can view this tutorial: Then we can use code below to make a python class instance callable.

Is it possible to make a custom instance object callable?

Unfortunately, no luck. As shown above, we weren’t able to call the object, consistent with which, using the callable function revealed that the callability of the instance object was negative. You may wonder whether it’s possible to make a custom instance object callable.

What is a callable object?

A callable object is a data structure that behaves as both an object and a function. You can access and assign properties , as if it were a function. context. class method.

What is the difference between callable and not callable in JavaScript?

The callable() method takes only one argument, an object and returns one of the two values: returns True, if the object appears to be callable. returns False, if the object is not callable.


2 Answers

Changing tick(self) to __call__(self) is the correct solution.

This has nothing to do with memory allocation. All __call__ signifies is the function which Python calls when you use (for an object foo) the syntax foo().

For your later question: to check whether something is an object, use isinstance or issubclass (possibly with the object class). And in my mind this answer is better than the accepted one for the "How do I determine if something is a function?" question you've probably seen.

like image 145
Borealid Avatar answered Oct 17 '22 12:10

Borealid


To make a class instance callable, all you need to do is implement a __call__ method. Then, to call the __call__ method, you just do: instance(arg1,arg2,...) -- in other words, you call the instance the same way you would call a function.

Note that you can alias __call__ with some other method defined on the class if you want:

>>> class Foo(object):
...     def tick(self):
...         print ("something")
...     __call__ = tick
... 
>>> a = Foo()
>>> a()
something
>>> a.tick()
something
like image 41
mgilson Avatar answered Oct 17 '22 12:10

mgilson