Python - how can I override the functionality of a class before its imported by a different module?

2024/9/30 13:31:38

I have a class that's being imported in module_x for instantiation, but first I want to override one of the class's methods to include a specific feature dynamically (inside some middleware that runs before module_x is loaded.

Answer

Neither AndiDog's nor Andrew's answer answer your question completely. But they have given the most important tools to be able to solve your problem (+1 to both). I will be using one of their suggestions in my answer:

You will need 3 files:

File 1: myClass.py

class C:def func(self):#do something

File 2: importer.py

from myClass import *
def changeFunc():A = C()A.func = lambda : "I like pi"return Aif __name__ == "importer":A = changeFunc()

File 3: module_x.py

from importer import *
print A.func()

The output of module_x would print "I like pi"

Hope this helps

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

Related Q&A

Calling a stateful LSTM as a functional model?

I have a stateful LSTM defined as a Sequential model:model = Sequential() model.add(LSTM(..., stateful=True)) ...Later, I use it as a Functional model:input_1, input_2 = Input(...), Input(...) output_1…

How to cluster Gantt bars without overlap?

Using create_gantt I have overlapping start and end dates: import plotly.plotly as py import plotly.figure_factory as ff import plotlydf = [dict(Task="Milestone A", Start=2017-01-01, Finish=2…

Fail to install lxml using pip

This is the command I used to install lxml:sudo pip install lxmlAnd I got the following message in the Cleaning Up stage:Cleaning up... Command /usr/bin/python -c "import setuptools, tokenize;…

Python 3.x list comprehension VS tuple generator

Is there any reason for memory, speed or whatever, that I would want to use:tuple(i for i in range(5000))instead of:[i for i in range(5000)]If I didnt mind the immutability of tuples

Using Sphinx with a distutils-built C extension

I have written a Python module including a submodule written in C: the module itself is called foo and the C part is foo._bar. The structure looks like:src/ foo/__init__.py <- contains the public …

PyYAML error: Could not determine a constructor for the tag !vault

I am trying to read a YAML file that has the tag !vault in it. I get the error:could not determine a constructor for the tag !vaultUpon reading a couple of blogs, I understood that I need to specify so…

Accessing a XAMPP mysql via Python

Im attempting to use mysql after only having worked with sqlite in the past.Ive installed XAMPP on Linux (ubuntu) and have mysql up and running fine (seems like that with phpMyadmin at least). However,…

How to use deep learning models for time-series forecasting?

I have signals recorded from machines (m1, m2, so on) for 28 days. (Note: each signal in each day is 360 length long).machine_num, day1, day2, ..., day28 m1, [12, 10, 5, 6, ...], [78, 85, 32, 12, ...],…

Python - Create Shortcut with arguments

Using win32com.client, Im attempting to create a simple shortcut in a folder. The shortcut however I would like to have arguments, except I keep getting the following error.Traceback (most recent call …

How to replace a range of values with NaN in Pandas data-frame?

I have a huge data-frame. How should I replace a range of values (-200, -100) with NaN?