Just subclass and override whatever method you wish to modify “Private” is conceptual. Mostly it means when the next version of a module comes out, code that you wrote that accesses *._ parts of the module might break. ___
import pandas class MyClass(pandas.ExcelFile.OpenpyxlReader): def _convert_cell(self, cell, convert_float: bool) -> 'Scalar': """override""" # do whatever you want, or call the base class version return super()._convert_cell(cell, convert_float) — Gerard Weatherby | Application Architect NMRbox | NAN | Department of Molecular Biology and Biophysics UConn Health 263 Farmington Avenue, Farmington, CT 06030-6406 uchc.edu On Sep 17, 2022, 5:29 PM -0400, Ralf M. <ral...@t-online.de>, wrote: *** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. *** Am 17.09.2022 um 00:35 schrieb Dan Stromberg: On Fri, Sep 16, 2022 at 2:06 PM Ralf M. <ral...@t-online.de <mailto:ral...@t-online.de>> wrote: I would like to replace a method of an instance, but don't know how to do it properly. You appear to have a good answer, but... are you sure this is a good idea? It's definitely a dirty hack. It'll probably be confusing to future maintainers of this code, and I doubt static analyzers will like it either. I agree that I will have to add sufficient comments for the future maintainer, should there ever be one (and even for me to still understand it next year). I don't use static analyzers. I'm not the biggest fan of inheritance you'll ever meet, but maybe this is a good place for it? Using a derived version of the class in question to overwrite the method was my first idea, however I don't instantiate the class in question myself, it is instantiated during the initialisation of another class, so I would at least have to derive a modified version of that as well. And that code is rather complex, with metaclasses and custom decorators, and I feel uncomfortable messing with that, while the method I intend to change is quite simple and straightforward. In case anybody is interested what I'm trying to achieve: It's simple in pandas to read an excel file into a dataframe, but only the cell value is read. Sometimes I need more / other information, e.g. some formatting or the hyperlink in a cell. Reopening the file with openpyxl and getting the info is possible, but cumbersome. Looking into the pandas code for reading excel files (which uses openpyxl internally) I noticed a method (of an internal pandas class) that extracts the value from an openpyxl cell. This method is rather simple and seems the ideal spot to change to get what I want. My idea is to instantiate pandas.ExcelFile (official pandas API), get the reader instance (an attribute of the ExcelFile object) and modify the method of the reader instance. The fact that the method I change and the ExcelFile attribute containing the reader are both private (start with _) doesn't make it any better, but I'm desperate enough to be willing to adapt my code to every major pandas release, if necessary. Ralf M. -- https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!mYWFkAugwhU4HgCv9nRg1vSJhyJCA8RApcnyGTRNGQYTTmvVigqANAagTbBwo96YFdHmzfCYU8gN3KpVmcrmOg$ -- https://mail.python.org/mailman/listinfo/python-list