optional-arguments

Why is the empty dictionary a dangerous default value in Python?

Why is the empty dictionary a dangerous default value in Python? Question: I put a dict as the default value for an optional argument to a Python function, and pylint (using Sublime package) told me it was dangerous. Can someone explain why this is the case? And is a better alternative to use None instead? …

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

Python: argparse optional arguments without dashes

Python: argparse optional arguments without dashes Question: I would like to have the following syntax: python utility.py file1 FILE1 file2 FILE2 where file1 and file2 are optional arguments. It is simple to make it working with this syntax: python utility.py –file1 FILE1 –file2 FILE2 using parser.add_argument(‘–file1’,type=file) parser.add_argument(‘–file2’,type=file) however, if I remove the dashes, argparse starts …

Total answers: 3

How do I create a Python function with optional arguments?

How do I define a function with optional arguments? Question: I have a Python function which takes several arguments. Some of these arguments could be omitted in some scenarios. def some_function (self, a, b, c, d = None, e = None, f = None, g = None, h = None): #code The arguments d through …

Total answers: 7