How to find class of bound method during class construction in Python 3.1?

2024/10/13 23:17:20

i want to write a decorator that enables methods of classes to become visible to other parties; the problem i am describing is, however, independent of that detail. the code will look roughly like this:

def CLASS_WHERE_METHOD_IS_DEFINED( method ):???def foobar( method ):print( CLASS_WHERE_METHOD_IS_DEFINED( method ) )class X:@foobardef f( self, x ):return x ** 2

my problem here is that the very moment that the decorator, foobar(), gets to see the method, it is not yet callable; instead, it gets to see an unbound version of it. maybe this can be resolved by using another decorator on the class that will take care of whatever has to be done to the bound method. the next thing i will try to do is to simply earmark the decorated method with an attribute when it goes through the decorator, and then use a class decorator or a metaclass to do the postprocessing. if i get that to work, then i do not have to solve this riddle, which still puzzles me:

can anyone, in the above code, fill out meaningful lines under CLASS_WHERE_METHOD_IS_DEFINED so that the decorator can actually print out the class where f is defined, the moment it gets defined? or is that possibility precluded in python 3?

Answer

When the decorator is called, it's called with a function as its argument, not a method -- therefore it will avail nothing to the decorator to examine and introspect its method as much as it wants to, because it's only a function and carries no information whatsoever about the enclosing class. I hope this solves your "riddle", although in the negative sense!

Other approaches might be tried, such as deep introspection on nested stack frames, but they're hacky, fragile, and sure not to carry over to other implementations of Python 3 such as pynie; I would therefore heartily recommend avoiding them, in favor of the class-decorator solution that you're already considering and is much cleaner and more solid.

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

Related Q&A

Multi-Threaded NLP with Spacy pipe

Im trying to apply Spacy NLP (Natural Language Processing) pipline to a big text file like Wikipedia Dump. Here is my code based on Spacys documentation example:from spacy.en import Englishinput = open…

Django Tastypie throws a maximum recursion depth exceeded when full=True on reverse relation.

I get a maximum recursion depth exceeded if a run the code below: from tastypie import fields, utils from tastypie.resources import ModelResource from core.models import Project, Clientclass ClientReso…

Adding a colorbar to two subplots with equal aspect ratios

Im trying to add a colorbar to a plot consisting of two subplots with equal aspect ratios, i.e. with set_aspect(equal):The code used to create this plot can be found in this IPython notebook.The image …

Why is C++ much faster than python with boost?

My goal is to write a small library for spectral finite elements in Python and to that purpose I tried extending python with a C++ library using Boost, with the hope that it would make my code faster. …

pandas: How to get .to_string() method to align column headers with column values?

This has been stumping me for a while and I feel like there has to be a solution since printing a dataframe always aligns the columns headers with their respective values.example:df = pd.DataFrame({Fir…

Do I need to use `nogil` in Cython

I have some Cython code that Id like to run as quickly as possible. Do I need to release the GIL in order to do this? Lets suppose my code is similar to this: import numpy as np# trivial definition ju…

supervisord environment variables setting up application

Im running an application from supervisord and I have to set up an environment for it. There are about 30 environment variables that need to be set. Ive tried putting all on one bigenvironment=line and…

Updating gui items withing the process

I am trying to make a GUI for my app and ran into a problem: using PySimpleGUI I have to define layout at first and only then display the whole window. Right now the code is like this:import PySimpleGU…

UnicodeDecodeError with Djangos request.FILES

I have the following code in the view call..def view(request):body = u"" for filename, f in request.FILES.items():body = body + Filename: + filename + \n + f.read() + \nOn some cases I getU…

bifurcation diagram with python

Im a beginner and I dont speak english very well so sorry about that. Id like to draw the bifurcation diagram of the sequence : x(n+1)=ux(n)(1-x(n)) with x(0)=0.7 and u between 0.7 and 4.I am supposed …