making a class callable in same instance
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.
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