0

I am writing a function to plot a graph. The problem is, there are restrictions on the inputs of the function that I am unsure how to put into code. For example, in

def PlotGraph(data, sex, c_list, status='current'):

The restrictions that I need to impose are the following:

  1. The PlotGraph function must accept either sex or c_list as in input, but not both. Sex is a string that can only be 'male' or 'female' and c_list is a list of integers.

  2. Status must be either 'current' or 'previous', but defaulted at 'current', which I have implemented.

For (1) I have searched around but cannot find a method as to how only one of the two types of data can be inputted AND to impose what the variable type of either input must be. For (2) I have an idea to include in the function

if status != 'current' or 'previous': 
    print("Invalid input") 

But I'm not sure if this will work if no input is submitted for status. If you have any ideas on how I can implement the stated restrictions, it would be much appreciated. Thank you.

Błażej Michalik
  • 4,474
  • 40
  • 55
Coconut
  • 29
  • 10
  • 1
    This strikes me as [an XY problem](https://meta.stackexchange.com/q/66377/322040). If the arguments are mutually exclusive and of unrelated types, it's almost certain there should be more than one function (if a lot of the behavior is the same, a third, private function with common functionality called by the other two after they massage their arguments to common form makes sense). Also, `if status != 'current' or 'previous':` [doesn't do what you think it does](https://stackoverflow.com/q/20002503/364696). – ShadowRanger Nov 19 '20 at 00:46
  • I'm not too familiar with what an 'xy' problem is. I am not too experienced with Python, there is a question I have about imposing multiple conditions and I wanted to show in my post that I made some sort of attempt at tackling the problem. Thank you for pointing out problem with my status condition line, that's something I should take note of! – Coconut Nov 19 '20 at 00:51
  • Read the link on XY problem. Short version: You're trying to accomplish goal X, and decide to solve it via solution Y. Solution Y is a *bad* solution, and you can't make it work, but ask how to fix your solution Y, instead of looking for a better solution to accomplish goal X. In this case, you're trying to figure out how to make mutually exclusive arguments to shove loosely related functionality into a single function, when the solution is almost certainly to not shove all this functionality into a single function (thereby removing the issue with making arguments mutually exclusive). – ShadowRanger Nov 19 '20 at 01:07
  • Thank you for your suggestion. I think you're reading way too much into my approach. I have a question on how to impose conditions, I try my best to answer as much of it as I can, and I learn any new techniques introduced to me from the answers. It's not wise for you to assume what I do or don't know, I am still very inexperienced with Python. Thanks nonetheless, I'll keep that article in mind, – Coconut Nov 19 '20 at 01:22

1 Answers1

0

In your given function signature, you have both sex and c_list as required positional arguments; this signature prohibits accepting a call with only one of them. Instead, try a single argument for both:

def PlotGraph(data, key, status='current'):
    if type(key) == int:
        sex = key
    elif type(key) == list:
        c_list = key
    else:
        print ("invalid key argument")

YOu have the right idea for checking status, except that you need to learn how to check against multiple values.

Prune
  • 76,765
  • 14
  • 60
  • 81