Python function optional input

Python function optional input

Posted: Alex Poster On: 16.07.2017

I'm writing a python script to find restrictionplaces. I was wondering if there is a better way to write the following:. This question appears to be off-topic. The users who voted to close gave this specific reason: Pseudocode, hypothetical code, or stub code should be replaced by a concrete implementation.

Using Optional and Named Arguments

Questions seeking an explanation of someone else's code are also off-topic. This means that if the caller of the function doesn't provide a value for that parameter the default of an empty string will be used instead.

Here is what you might write instead:. There is a caveat to default values when using mutables, however. While freebie's answer works and is a very common solution to this problem , I prefer an alternate approach that using None and or chaining:.

This works because the or operator in Python returns its left hand side if it is truthy , and otherwise returns its right hand side. Since None is falsy in Python, when the arguments are left as their defaults, these expressions replace themw ith their "true" default values.

The advantage to this is that callers to the function never have to know what a "default" value shoudl be for an argument; if they want to default it, they can pass it as None. This is useful for example when writing wrapper functions:. The advantage here is that using "true" default arguments, the wrapper function would either have to know the defaults that the base function uses for its arguments which makes it easy to break the code accidentally by changing the default arguments in the base class , or it would have to branch to omit the arguments entirely when they weren't specified.

Sign up or log in to customize your list.

Visualize Python, Java, JavaScript, TypeScript, and Ruby code execution

Stack Exchange Inbox Reputation and Badges. Questions Tags Users Badges Unanswered. Code Review Stack Exchange is a question and answer site for peer programmer code reviews.

python function optional input

Join them; it only takes a minute: Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top. Optional arguments in a function [closed]. I was wondering if there is a better way to write the following: Here is what you might write instead: Didn't know this was also possible to be honest, quite new to programming: I'll update my answer and explain it a little better.

Python Input Output (I/O) Using input() and print() Function

There is a caveat to doing this. While freebie's answer works and is a very common solution to this problem , I prefer an alternate approach that using None and or chaining: This is useful for example when writing wrapper functions: Unfortunately, I don't think Python provides any elegant way of handling those case without extra branching which is that the OP already has in his original post.

Code Review Stack Exchange works best with JavaScript enabled. MathOverflow Mathematics Cross Validated stats Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more 3. Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent.

Rating 4,3 stars - 363 reviews
inserted by FC2 system