Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python – passing arguments to a function

I’m new to python actually started watching a tutorial, I know decent JavaScript, now here are my if statements

if str(fileType) == "V":
    if (not windowTitle) and (not windowSize):
        playVideo()
    elif (windowTitle) and (windowSize):
        playVideo(windowSize=int(windowSize), windowTitle=windowTitle)
    elif (windowSize):
        playVideo(int(windowSize=windowSize))
    elif (windowTitle):
        playVideo(windowTitle=windowTitle)

I want to know that if you want to skip the first argument and give second argument in python how would you do that, and I am giving arguments by writing their name = and the variable is that okay?

and after I check fileType I want to check if windowTitle and windowSize are empty am I doing it right?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

and also I don’t know what’s in a variable if user skips (hits enter without writing anything), like in JavaScript it would be undefined

>Solution :

You are correctly using the syntax to pass arguments to a method in python. There are two two ways to pass arguments to a method:

  1. By passing the argument’s value directly – you need to pass the arguments in order.
  2. By passing the argument’s name-value pair (as you’ve used before) – the arguments do not need to be in order as long you’ve specified the name value pair. This is also known as keyword arguments.

If you want to skip first argument and only pass the second argument, you can use keyword arguments (mentioned in 2 above). Something like – playVideo(windowTitle='My Video')

Also if the user skips to pass an argument, the value of that argument will be None

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading