Jump to content

[Python] Optional variables in funtion

Wictorian

How can I add an optional variable to a function in python?

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Wictorian said:

Do I have to specify the optional variables as 



variable = None

?

def function(<variable name> = None):
    your code

 

No, you don't have to specify the variable after that.

Edited by RockSolid1106
On 4/5/2024 at 10:13 PM, LAwLz said:

I am getting pretty fucking sick and tired of the "watch something else" responses. It's such a cop out answer because you could say that about basically anything, and it doesn't address the actual complaints. People use it as some kind of card they pull when they can't actually respond to the criticism raised but they still feel like they need to defend some company/person. If you don't like this thread then stop reading it. See how stupid it is? It's basically like telling someone "shut the fuck up". It's not a clever responsive, it doesn't address anything said, and it is rude. 

 ^

 

bruh switch to dark mode its at the bottom of this page

VPN Server Guide

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Wictorian said:

Do I have to specify the optional variables as 


variable = None

?

no, they are optional

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

38 minutes ago, Sauron said:

no, they are optional

What I meant is, do I have to specify it like this?

function(compulsoryVariable, optionalVariable = None):

Or is this okay ?

function(compulsoryVariable,optionalVariable):

 

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, Wictorian said:

What I meant is, do I have to specify it like this?


 

Yes, otherwise they won't be initialized to a value when you run the function...

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

19 hours ago, Sauron said:

Yes, otherwise they won't be initialized to a value when you run the function...

Can I specify it with anything other than None?

Link to comment
Share on other sites

Link to post
Share on other sites

Just some quick tips others might find helpful:

You must specify an initial value to your optional args, otherwise they're not optional since they need a value. 

They must be at the end of your functions' args as in:

 def foo (reqArg, reqArg1, reqArg2, optArg = 0, optArg1 = '', optArg2 = True):

And another way would be to pass an args list like:

def bar(reqArg, reqArg1, *args):

or have passed args as a dictionary with kwargs.

def foobar(reqArg, **kwargs):
	# get is used since it allows us to define a default value (if the kwarg wasn't passed)
	print(f"{reqArg} {kwargs.get('message', '')}")

foobar("hello", message="world")
foobar("goodbye")

Output:

hello world
goodbye

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×