optional-parameters

Python mock.patch.object with functool.partial bound arguments possible?

Python mock.patch.object with functool.partial bound arguments possible? Question: How to solve this? Patch a objects method with another signature (eg. an additional argument. I’ve tried to bound the optional argument, but this does not seem to work. I can not use plain monkey patching here, since the the patched class is called under in a …

Total answers: 3

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

How to check whether optional function parameter is set

How to check whether optional function parameter is set Question: Is there an easy way in Python to check whether the value of an optional parameter comes from its default value, or because the user has set it explicitly at the function call? Asked By: Matthias || Source Answers: Not really. The standard way is …

Total answers: 10

Optional parameters in functions and their mutable default values

Optional parameters in functions and their mutable default values Question: Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I’m kind of confused about how optional parameters work in Python functions/methods. I have the following code block: >>> def F(a, b=[]): … b.append(a) … return b … >>> F(0) [0] >>> F(1) [0, 1] …

Total answers: 2

Normal arguments vs. keyword arguments

Normal arguments vs. keyword arguments Question: How are “keyword arguments” different from regular arguments? Can’t all arguments be passed as name=value instead of using positional syntax? Asked By: mk12 || Source Answers: There are two ways to assign argument values to function parameters, both are used. By Position. Positional arguments do not have keywords and …

Total answers: 10