I am writing some plotting routines in python. At present I can a function with some parameters and the plot function decides the best x-axis and y-axis scale to use. Is there a way for me to modify my function definition such that if I provide two extra arguments, say in the form of two lists:

xrange=[-10,10] & yrange[0,10]

i.e. the function call would be MyPlot([Usual stuff], xrange, yrange) and the plot would know to use xrange and yrange but when I didn't include these additional arguments i.e. my function call was simply MyPlot([Usual stuff]) the function would carry on automatically deciding my scale? Cheers, Jack

有帮助吗?

解决方案

You can define your function like this

MyPlot([Usual stuff], xrange = None, yrange = None):
   if xrange != None and yrange != None:
       do special stuff
   else:
       ordinary stuff

Now, xrange and yrange accept None as default parameters. So, that you can ignore them while calling them. If those parameters are ignored, by default None will be assigned to those variables.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top