Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Python callable() Explained

In programming, a Callable is something that can be called. In Python, a callable is anything that can be called, using parentheses and maybe with some arguments. Functions, Generators, and Classes are inherently callable in Python. The callable() method takes an object and returns a boolean. True – if the object is callable False – if the object is not callable The callable() method checks if the object is either of the two – An instance of a class with a __call__ method Is of a type that has a which indicates callability such as in functions, classes, etc. or has a non-null tp_call (c struct) member. Since functions are callable in Python. def my_function(): print("Hi, I'm a function") callable(my_function) Output True This indicates that every time we create a function, Python creates a callable object for it. You can also verify the presence of __call__ attribute. my_function.__call__ Output Similarly for a Class, class MyClass(): def my_method(self): print("Hi, I am a class method") callable(MyClass) Output True However, if you create an object and …

The post Python callable() Explained appeared first on Django Central.



This post first appeared on Django Central, please read the originial post: here

Share the post

Python callable() Explained

×

Subscribe to Django Central

Get updates delivered right to your inbox!

Thank you for your subscription

×