Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Byte your tongue.


devel / comp.lang.python / Re: How to replace an instance method?

SubjectAuthor
o Re: How to replace an instance method?Ralf M.

1
Re: How to replace an instance method?

<mailman.454.1663450041.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19535&group=comp.lang.python#19535

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!news.szaf.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: Ral...@t-online.de (Ralf M.)
Newsgroups: comp.lang.python
Subject: Re: How to replace an instance method?
Date: Sat, 17 Sep 2022 23:21:08 +0200
Lines: 53
Message-ID: <mailman.454.1663450041.20444.python-list@python.org>
References: <f9f8bac0-9a27-8615-6164-38aa2d9e0fbb@t-online.de>
<CAGGBd_qKS9D6bHQ=ZwDX54e-uxWG-c4H+u6aYYAPk4oMENhgww@mail.gmail.com>
<5f661dae-4741-7e67-4d18-f2671b9fd49d@t-online.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de IPmG9rXqO7UjFwu59iZOFwPOuslfQtBtVHwSo4FsD2NA==
Return-Path: <Ralf_M@t-online.de>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'comments': 0.03; '(which':
0.04; '2022': 0.05; 'class,': 0.05; 'containing': 0.05; 'e.g.':
0.07; 'maintainers': 0.07; 'adapt': 0.09; 'better,': 0.09;
'cell.': 0.09; 'dan': 0.09; 'derived': 0.09; 'pandas': 0.09;
'schrieb': 0.09; 'possible,': 0.15; 'derive': 0.16; 'extracts':
0.16; 'instance': 0.16; 'properly.': 0.16; 'spot': 0.16; 'static':
0.16; 'wrote:': 0.16; 'probably': 0.17; 'uses': 0.19; '16,': 0.19;
'it?': 0.19; 'to:addr:python-list': 0.20; 'doubt': 0.22; 'fri,':
0.22; 'maybe': 0.22; 'version': 0.23; 'code': 0.23; 'subject:How':
0.23; 'received:de': 0.23; 'idea': 0.24; '(and': 0.25; 'seems':
0.26; 'intend': 0.26; 'fact': 0.28; 'header:User-Agent:1': 0.30;
'code,': 0.31; 'modify': 0.31; "doesn't": 0.32; 'question': 0.32;
'anybody': 0.32; 'modified': 0.32; 'sep': 0.32; 'but': 0.32;
"i'm": 0.33; 'there': 0.33; 'header:In-Reply-To:1': 0.34;
'trying': 0.35; 'definitely': 0.35; 'files': 0.36; 'cell': 0.36;
'change': 0.36; 'using': 0.37; "it's": 0.37; 'class': 0.37;
'received:192.168': 0.37; 'file': 0.38; 'read': 0.38; 'quite':
0.39; 'least': 0.39; 'enough': 0.39; 'use': 0.39; 'still': 0.40;
'both': 0.40; 'should': 0.40; 'method': 0.61; 'internal': 0.63;
'ever': 0.63; 'feel': 0.63; 'willing': 0.64; 'that,': 0.67;
'interested': 0.68; 'during': 0.69; 'excel': 0.69;
'subject:method': 0.69; 'received:194': 0.70; 'future': 0.72;
'private': 0.73; "you'll": 0.73; 'formatting': 0.76; 'major':
0.78; 'biggest': 0.84; 'attribute': 0.84; 'inheritance': 0.84;
'want.': 0.84; 'fan': 0.91
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.2.2
In-Reply-To: <CAGGBd_qKS9D6bHQ=ZwDX54e-uxWG-c4H+u6aYYAPk4oMENhgww@mail.gmail.com>
X-TOI-EXPURGATEID: 150726::1663449671-6AFFD9DD-A0FCBB56/0/0 CLEAN NORMAL
X-TOI-MSGID: ae08c62b-adf0-420c-bfa0-67a410661050
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <5f661dae-4741-7e67-4d18-f2671b9fd49d@t-online.de>
X-Mailman-Original-References: <f9f8bac0-9a27-8615-6164-38aa2d9e0fbb@t-online.de>
<CAGGBd_qKS9D6bHQ=ZwDX54e-uxWG-c4H+u6aYYAPk4oMENhgww@mail.gmail.com>
 by: Ralf M. - Sat, 17 Sep 2022 21:21 UTC

Am 17.09.2022 um 00:35 schrieb Dan Stromberg:
>
>
> On Fri, Sep 16, 2022 at 2:06 PM Ralf M. <Ralf_M@t-online.de
> <mailto:Ralf_M@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.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor