0

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

BenMorel
  • 34,448
  • 50
  • 182
  • 322
JMzance
  • 1,704
  • 4
  • 30
  • 49

1 Answers1

2

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.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • Would these be considered 'positional arguments'? i.e. if I were to call MyPlot([Usual], [0,10]) would that assume the scale I wanted to set was x or y? Or should I actually be calling the new function as MyPlot([Usual], yrange=[0,10]) for example? – JMzance Nov 06 '13 at 11:55
  • Also having built a quick implementation as you suggested shouldn't the conditional statement be like : if xrange != 'None ... I get errors if I dont include the inverted commas – JMzance Nov 06 '13 at 12:00
  • @JackMedley Please edit your question with the latest code which you have. – thefourtheye Nov 06 '13 at 12:05