Invoking the __call__ method of a superclass

2024/10/14 21:19:09

http://code.google.com/p/python-hidden-markov/source/browse/trunk/Markov.py

Contains a class HMM, which inherits from BayesianModel, which is a new-style class. Each has a __call__ method. HMM's __call__ method is meant to invoke BayesianModel's at line 227:

return super(HMM,self)(PriorProbs)

However, this fails with an exception

super(HMM,self)

is not callable.

What am I doing wrong?

Answer

You need to invoke the __call__ method itself, explicitly:

return super(HMM, self).__call__(PriorProbs)

This applies to any hook that needs to call the overridden method on the superclass.

super() returns a proxy object, with a .__getattribute__() method that searches the super-class hierarchy for the attribute you are searching for. This proxy itself is not callable; it has no __call__ method of it's own. Only when you explicitly look up the __call__ method as an attribute of that proxy can python find the right implementation for you.

https://en.xdnf.cn/q/69369.html

Related Q&A

Efficent way to split a large text file in python [duplicate]

This question already has answers here:Sorting text file by using Python(3 answers)Closed 10 years ago.this is a previous question where to improve the time performance of a function in python i need t…

Creating a unique id in a python dataclass

I need a unique (unsigned int) id for my python data class. This is very similar to this so post, but without explicit ctors. import attr from attrs import field from itertools import count @attr.s(aut…

How to get all the models (one for each set of parameters) using GridSearchCV?

From my understanding: best_estimator_ provides the estimator with highest score; best_score_ provides the score of the selected estimator; cv_results_ may be exploited to get the scores of all estimat…

How do I perform deep equality comparisons of two lists of tuples?

I want to compare two lists of tuples:larry = [(1,a), (2, b)] moe = [(2, b), (1, a)]such that the order of the items in the list may differ. Are there library functions to do this ?>> deep_equal…

Metadata-generation-failed when trying to install pygame [duplicate]

This question already has answers here:Python pygame not installing(3 answers)Closed last year.Trying to install pygame on python 3.11 using the following command "pip install pygame" and I a…

Why such a big pickle of a sklearn decision tree (30K times bigger)?

Why pickling a sklearn decision tree can generate a pickle thousands times bigger (in terms of memory) than the original estimator? I ran into this issue at work where a random forest estimator (with …

Buffer size for reading UDP packets in Python

I am trying to find out / adjust the size of network buffers:import socketsock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)sock.getsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF) 212992What on earth i…

Why does datetime give different timezone formats for the same timezone?

>>> now = datetime.datetime.now(pytz.timezone(Asia/Tokyo)) >>> dt = datetime(now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond, pytz.timezone(Asia/Tokyo)…

Connect with pyppeteer to existing chrome

I want to connect to an existing (already opened, by the user, without any extra flags) Chrome browser using pyppeteer so I would be able to control it. I can do almost every manual action before (for …

Combining asyncio with a multi-worker ProcessPoolExecutor and for async

My question is very similar to Combining asyncio with a multi-worker ProcessPoolExecutor - however a slight change (I believe its the async for) makes the excellent answers there unusuable for me.I am …