0

I have a below function where infile is the parameter of func. Now I want to perform the below operation.

If infile = default, then do, df = pd.read_csv("/path/to/the/file/given/a.csv) else, read the file path given to the infile.

def func(infile):
  if infile == default:
      df = pd.read_csv("/path/to/the/file/given/a.csv)
  else:
      df = pd.read_csv(infile)

This approach is working fine

1
  • What is the value of default? Commented Oct 31, 2019 at 3:48

1 Answer 1

1

Default parameter in Python function definition works like below:

def func(infile="/path/to/the/file/given/a.csv"):
    df = pd.read_csv(infile)

When one or more parameters have the form parameter = expression, the function is said to have “default parameter values.” For a parameter with a default value, the corresponding argument may be omitted from a call, in which case the parameter’s default value is substituted. If a parameter has a default value, all following parameters up until the “*” must also have a default value — this is a syntactic restriction that is not expressed by the grammar.

Sign up to request clarification or add additional context in comments.

2 Comments

I need to have an option for infile parameter. For example, if infile ='Y', do this else do that
just implement as what you said, what's the problem you're facing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.