Callable Modules

Callable modules

Special methods are only guaranteed to be called implicitly when they are defined on the type, not on the instance. (__call__ is an attribute of the module instance mod_call, not of <type 'module'>.) You can't add methods to built-in types.

https://docs.python.org/reference/datamodel.html#special-lookup

Callable modules with parameters

There's nothing special about the class you created (it's not even a ModuleType subclass), so there's nothing special about its __call__ method. If you want to call it with arguments, just add parameters to the __call__ definition:

import sys

class foo(object):
def __call__(self, x):
return f'callable, and called with {x}'

sys.modules[__name__] = foo()

And now, you can pass it an argument, exactly like any other callable object:

import foo

print(foo('hello'))

And the output is:

callable, and called with hello

From the comments, you tried to do this:

def __call__(a, self):
return a

But, like all methods in Python, __call__ wants self to come first. It doesn't care about the names (unless you call it with keyword arguments), just the order: the first parameter gets the receiver (the foo in foo('hello')), even if it you called that parameter a, and the second parameter gets the first normal argument (the 'hello'), even if you called that parameter self.

So, you're passing the module foo as the first parameter, a, and you return a, so it returns foo.

Which is why you got this:

<sta.foo object at 0x10faee6a0>

That isn't an error, that's the perfectly valid output that you get when you print out an instance of a class that doesn't define __repr__ or __str__.

How to create a python module as a single callable function?

Unfortunately, this isn't possible.

An object, class, or function can be callable, but a module cannot.

You can, however, name things conveniently:

[greet.py]
def greet(x):
...

[main.py]
from greet import greet
greet('foo')

mediapipe.mp_holistic 'module' object is not callable

You are using the mp.solutions.holistic as function instead of module.
I think, somewhere in your code, you have:

mp_holistic = mp.solutions.holistic

Considering your snippet, I think you want to use the holistic call.

Consequently you have to change your:

with mp_holistic(min_detection_confidence=0.5,min_tracking_confidence=0.5) as holistic:

with

with mp_holistic.Holistic(min_detection_confidence=0.5,min_tracking_confidence=0.5) as holistic:

Why do I get the 'module' not callable error?

The MatPlotLib ([MatPlotLib]: API Reference) import statement should be:

import matplotlib.pyplot as plt

or

from matplotlib import pyplot as plt

The beauty of this situation (which makes the error harder to find) is a coincidence. figure name exists under both:

  • matplotlib - as a module

  • matplotlib.pyplot - as a function (this is the needed one)

Example:

>>> import matplotlib as mpl
>>>
>>> mpl
<module 'matplotlib' from 'e:\\Work\\Dev\\VEnvs\\py_pc064_03.09_test0\\lib\\site-packages\\matplotlib\\__init__.py'>
>>> mpl.figure
<module 'matplotlib.figure' from 'e:\\Work\\Dev\\VEnvs\\py_pc064_03.09_test0\\lib\\site-packages\\matplotlib\\figure.py'>
>>>
>>>
>>> import matplotlib.pyplot as plt
>>>
>>> plt
<module 'matplotlib.pyplot' from 'e:\\Work\\Dev\\VEnvs\\py_pc064_03.09_test0\\lib\\site-packages\\matplotlib\\pyplot.py'>
>>> plt.figure
<function figure at 0x000001C31C139D30>


Related Topics



Leave a reply



Submit