Monday, September 7, 2020

Abhijeet Pal: 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 <method-wrapper '__call__' of function object at 0x7f08706d5840> 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.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...