Copy signature, forward all arguments from wrapper function

2024/10/11 0:30:20

I have two functions in a class, plot() and show(). show(), as convenience method, does nothing else than to add two lines to the code of plot() like

def plot(self,show_this=True,show_that=True,color='k',boundary_color=None,other_color=[0.8, 0.8, 0.8],show_axes=True):# lots of codereturndef show(self,show_this=True,show_that=True,color='k',boundary_color=None,other_color=[0.8, 0.8, 0.8],show_axes=True):from matplotlib import pyplot as pltself.plot(show_this=show_this,show_that=show_that,color=color,boundary_color=boundary_color,other_color=other_color,show_axes=show_axes)plt.show()return

This all works.

The issue I have is that this seems way too much code in the show() wrapper. What I really want: Let show() have the same signature and default arguments as plot(), and forward all arguments to it.

Any hints?

Answer

Python 3 offers the ability to actually copy the signature of the wrapped function with the inspect module:

def show(self, *args, **kwargs):from matplotlib import pyplot as pltself.plot(*args, **kwargs)plt.show()
show.__signature__ = inspect.signature(plot)

Now if you use show in a shell that provides autocompletion like IDLE, you will see the correct parameters for show instead of the cryptics *args, **kwargs

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

Related Q&A

How to fit a line through a 3D pointcloud?

I have a cable I am dropping from moving vehicle onto the ground. Using a camera system I estimate the location where the rope touches the ground in realtime. Movement of the vehicle and inaccuracy in …

Websockets with Django Channels on Heroku

I am trying to deploy my app to heroku. The app has a simple chatting system that uses Websockets and django channels. When I test my app using python manage.py runserver the app behaves just as intend…

How can I get the name/file of the script from sitecustomize.py?

When I run any Python script, I would like to see the scripts filename appear in the Windows command line windows titlebar. For example, if I run a script called "mytest.py", I want to see &q…

Sending Godaddy email via Django using python

I have these settings EMAIL_HOST = smtpout.secureserver.net EMAIL_HOST_USER = [email protected] EMAIL_HOST_PASSWORD = password DEFAULT_FROM_EMAIL = [email protected] SERVER_EMAIL = [email protected] EM…

python math, numpy modules different results?

I get slightly different results calculating the cosine of a value. How can I check that this difference is within machine precision?import math math.cos(60.0/180.0*math.pi) -> 0.5000000000000001im…

How to extract equation from a polynomial fit?

My goal is to fit some data to a polynomial function and obtain the actual equation including the fitted parameter values. I adapted this example to my data and the outcome is as expected. Here is my c…

python dictionary conundrum

On the console I typed in>>> class S(str): pass ... >>> a = hello >>> b = S(hello) >>> d = {a:a, b:b} >>> d {hello: hello} >>> type(d[a]) <class…

Celery 4 not auto-discovering tasks

I have a Django 1.11 and Celery 4.1 project, and Ive configured it according to the setup docs. My celery_init.py looks likefrom __future__ import absolute_importimport osfrom celery import Celery# set…

Is it possible to use a custom filter function in pandas?

Can I use my helper function to determine if a shot was a three pointer as a filter function in Pandas? My actual function is much more complex, but i simplified it for this question.def isThree(x, y…

what is request in Django view

In the Django tutorial for the first app in Django we havefrom django.http import HttpResponsedef index(request):return HttpResponse("Hello, world. Youre at the polls index.")And then the url…