Override a library function that is not in __all__ - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Override a library function that is not in __all__ (/thread-34585.html) |
Override a library function that is not in __all__ - Weird - Aug-11-2021 Greetings Community, I would like to override a function of the library of networkx that is not included in __all__ .More specifically: import networkx as nx def geometric_edges(G, radius, p): return [] ... nx.generators.geometric.geometric_edges = geometric_edges return nx.random_geometric_graph(number, radius, pos=pos)I also tried: import networkx as nx from unittest.mock import patch as patch def geometric_edges(G, radius, p): return [] ... patch("nx.generators.geometric.geometric_edges", geometric_edges) return nx.random_geometric_graph(number, radius, pos=pos)Both attempts did fail so the original function will still be executed instead of the replacement returning an empty list which should lead to an unconnected graph. Could you possibly explain to me which ways I have to achieve my goal and how they are executed correctly? Thank you very much in advance. Kind regards, Weird RE: Override a library function that is not in __all__ - Larz60+ - Aug-11-2021 you need to inherit class you wish to override. There are many good examples, here's one: https://www.geeksforgeeks.org/method-overriding-in-python/ RE: Override a library function that is not in __all__ - Weird - Aug-12-2021 (Aug-11-2021, 03:24 PM)Larz60+ Wrote: you need to inherit class you wish to override. Sorry, I forgot to add a git reference: https://github.com/networkx/networkx/blob/main/networkx/generators/geometric.py The module is not a class but a collection of functions. Therefore my two approaches above that didn't work. I hope the git reference helps you to help me Also "override" might not be the correct technical term I assume it pretty much describes my intention. @Edit: I guess the lack of further comments signals a significant misunderstanding on my side - could somebody help me to notify it? RE: Override a library function that is not in __all__ - Weird - Aug-14-2021 I don't know whether it is okay to push your thread after some days. So I'll try and in case it is not, I will know for future interactions in this forum. RE: Override a library function that is not in __all__ - Larz60+ - Aug-15-2021 see: https://dabeaz-course.github.io/practical-python/Notes/04_Classes_objects/02_Inheritance.html you need to create an instance of nx before you can redefine one of it's methods. see especially section labeled 'Redefining an existing method' RE: Override a library function that is not in __all__ - Weird - Aug-17-2021 Hi, didn't knew that it is possible in python to instantiate modules. Kind regards, Weird RE: Override a library function that is not in __all__ - Weird - Aug-23-2021 I would like to add a question regarding this topic: Is there a way to globally override the function - somehow export the instantiated module for all modules of my project? Or somehow create a new module that takes networkx overrides this method and allows me to import it as networky? RE: Override a library function that is not in __all__ - Larz60+ - Aug-23-2021 create a class, and then import into other modules. in list below, I should have pointed you there before, but forgot about these: more on classes: class basics class intermediate inheritance class advanced operator overloading |