default-value

Default arguments in a function when using decorators

Default arguments in a function when using decorators Question: I cannot really tell how to use *args and **kwargs when combined with decorators and default arguments. Let me give you a MWE. def outer(f): def inner(*args, **kwargs): print("args:", args) print("kwargs:", kwargs) return inner @outer def simple(x, y): pass Running the simple function passing arguments in …

Total answers: 2

How to apply default value to Python dataclass field when None was passed?

How to apply default value to Python dataclass field when None was passed? Question: I need a class that will accept a number of parameters, I know that all parameters will be provided but some maybe passed as None in which case my class will have to provide default values. I want to setup a …

Total answers: 9

Default values on empty user input

How to define default value if empty user input in Python? Question: Here I have to set the default value if the user will enter the value from the keyboard. Here is the code that user can enter value: input = int(raw_input(“Enter the inputs : “)) Here the value will be assigned to a variable …

Total answers: 7

How to make a list as the default value for a dictionary?

How to make a list as the default value for a dictionary? Question: I have a Python code that looks like: if key in dict: dict[key].append(some_value) else: dict[key] = [some_value] but I figure there should be some method to get around this ‘if’ statement. I tried: dict.setdefault(key, []) dict[key].append(some_value) and dict[key] = dict.get(key, []).append(some_value) but …

Total answers: 1

Python argparse: default value or specified value

Python argparse: default value or specified value Question: I would like to have a optional argument that will default to a value if only the flag is present with no value specified, but store a user-specified value instead of the default if the user specifies a value. Is there already an action available for this? …

Total answers: 2

Set a default choice for optionparser when the option is given

Set a default choice for optionparser when the option is given Question: I have a python option parsers that parses an optional –list-something option. I also want the –list-something option to have an optional argument (an option) Using the argument default=”simple” does not work here, otherwise simple will always be the default, not only when …

Total answers: 2

Named tuple and default values for optional keyword arguments

Named tuple and default values for optional keyword arguments Question: I’m trying to convert a longish hollow “data” class into a named tuple. My class currently looks like this: class Node(object): def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right After conversion to namedtuple it looks like: from collections import …

Total answers: 23

How does the Python range function have a default parameter before the actual one?

How does the Python range function have a default parameter before the actual one? Question: So I’m writing a function that takes an optional list and extends it to the length specified. Rather than writing it as foo(n, list=None) I was wondering how I might emulate the behavior of Python’s range function which works like: …

Total answers: 5

How does collections.defaultdict work?

Collections.defaultdict difference with normal dict Question: I’ve read the examples in python docs, but still can’t figure out what this method means. Can somebody help? Here are two examples from the python docs >>> from collections import defaultdict >>> s = ‘mississippi’ >>> d = defaultdict(int) >>> for k in s: … d[k] += 1 …

Total answers: 16