Tagged Questions
1
vote
3answers
36 views
Optional Parameters, certain combination of them required
I have a general question as well as a specific use case.
Optional parameters are easy enough: def func(a, b, c=None): ... and then anywhere c might be used in the body just write if c: first, or ...
1
vote
2answers
76 views
How to create a common interface function for implementations which require both common and specific parameters (in Python)?
Trying to write an interface function in Python that can be called on an an implementation while knowing the least amount of implementation-specific information about it as possible. This is a little ...
1
vote
1answer
85 views
Python: Understanding unfamiliar function syntax (from UC Berkeley CS61A)
I can't do most parts of this question without peeking at the answer key, so I figured I could ask it here. Frankly, I've always had a problem understanding functions with no parameters, and whenever ...
3
votes
4answers
100 views
Use global variables as default values
I have a function which takes a lot of parameters, and since I dont want to remember their positions I decided to use named arguments
def f(a=None, b=None, c=None):
print a,b,c
f('test', c=5, ...
0
votes
1answer
31 views
Functional dependence on optional arguments in python
I am writing some plotting routines in python. At present I can a function with some parameters and the plot function decides the best x-axis and y-axis scale to use. Is there a way for me to modify ...
3
votes
5answers
111 views
What is the most pythonic way to call a method with optional parameters?
Let's say I have a method with a few optional parameters.
def foo(a, b=1, c=2, d=3)
How do I go about calling it so that if my variables are None or empty strings the defaults are used?
...
2
votes
1answer
198 views
Python function optional arguments - possible to add as condition?
Is it possible, somehow, to aassign a condtional statement toan optional argument?
My initial attempts, using the following construct, have been unsuccessful:
y = {some value} if x == {someValue} ...
1
vote
2answers
79 views
Python default values for parameters in delegated function calls
There is a function with a default value for a parameter:
def g(y, z = "default"):
print y, z
I want to call this function from a function "f", that among others should also contain a ...
4
votes
1answer
258 views
python argparse to handle arbitrary numeric options (like HEAD(1))
Is there a way to trick argparse into accepting arbitrary numeric arguments like HEAD(1)?
head -5 test.txt
is equivalent to
head -n 5 test.txt
My current approach is to use parse_known_args() ...
0
votes
3answers
68 views
How to work with optional parameters that are set to None
With named parameters, how can I tell the receiver method to use the "not supplied" version of the parameter? Sending in None does not work. The following is my specific code, note especially the ...
2
votes
2answers
953 views
Trying to understand optional, list and named arguments in Python
I'm a beginner at Python, trying to understand function arguments and their types and orders.
I tried to experiment a little with the different kinds of argument and here's my experiment
def main():
...
39
votes
7answers
24k views
Python normal arguments vs. keyword arguments
Could someone explain the differences to me? Aren't all arguments "keyword arguments"? They all have names, and can all be assigned by that name instead of the position. Do keyword arguments mean ones ...
0
votes
2answers
86 views
python argparse strore_true and store optional option in one argument [duplicate]
I need to recognize if was given argument alone or with optional string or neither
parser.add_argument(???)
options = parser.parse_args()
so
./prog.py --arg
should store '' into options.arg,
...
13
votes
2answers
14k views
Optional parameters in Python functions and their default values [duplicate]
Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
I'm kind of confussed about how optional parameters work in Python functions/methods.
I have ...
5
votes
4answers
2k views
Python: how to check whether optional function parameter is set
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?
1
vote
2answers
334 views
Multi-parameter function - Correct usage of *args / **kwargs
I have a function like this one:
def hexify_string(aString):
#code for string2hexa conversion
...and I want to have a function who accepts one or more (quantity undefined) parameters, and to ...
0
votes
3answers
250 views
Python get string right after argument switch
I have a script that goes using os.walk to go down a directory structure and matching files by extension and copies files to another location.
This is what i have for the file copy:
...
2
votes
2answers
151 views
python classes/subclasses with mutually exclusive optional arguments using a instance of the superclass as a possible constructor
I am trying to define in python a class B, subclass of A, that could accept multiple ways of being instantiated to be very flexible. A is a simple class with few members. I know how to write A to ...
3
votes
1answer
545 views
python explicitly not passing optional parameter
imagine the following scenario:
class A:
def __init__(self, arg1=3, arg2=5):
pass
def createA(arg1=None, arg2=None):
if arg1 is None:
if arg2 is None:
a = A()
...
1
vote
4answers
548 views
How to define method with optional argument and argument unpacking?
I have a question regarding how to define and call a method with both optional arguments and with a *args parameter in Python. For example,
def test_func(arg1, optional_arg=None, *args):
...
...
5
votes
3answers
897 views
random.shuffle Randomness
I am trying to write a genetic algorithm for homework to solve the travelling salesman problem.
One of the mutation functions that I'm trying is to use random.shuffle on the tour.
When I read the ...
1
vote
3answers
86 views
Default array parameters unexpectedly not empty [duplicate]
Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
def stackdemo(stack=[]):
stack.append('q')
return stack
stackdemo()
print stackdemo()
...
0
votes
1answer
571 views
Optional Parameters in Python with List Append [duplicate]
Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
This is very odd, an optional list parameter in Python is persistent between function calls when ...
9
votes
5answers
284 views
Python - Better to have multiple methods or lots of optional parameters?
I have a class which makes requests to a remote API. I'd like to be able to reduce the number of calls I'm making. Some of the methods in my class make the same API calls (but for different reasons), ...
1
vote
3answers
679 views
Regex for matching optional parameters as groups in string
I am trying to read a batch script containing an input string of a command and optional parameters like
input = 'command -a field1 -b field2 -c field3'
The option parameters are set by a user in a ...
5
votes
1answer
969 views
Python Option Parser: Boolean flag with optional parameters
I'm using optparse.OptionParser to manage arguments for some scripts, and something I was wondering / would like to do is have boolean flags (i.e action=store_true) that can also accept a parameter.
...
5
votes
4answers
717 views
Assign pass to a function in Python
I have a piece of code that defines a function that takes a function as an argument, like so:
def stuff(n, f):
f(n)
Now, I want to provide some default value of f that does nothing. So I figured ...
2
votes
2answers
1k views
difference between default and optional arguments
okay code:
#!/usr/bin/python
import wx
import sys
class XPinst(wx.App):
def __init__(self, redirect=False, filename=None):
wx.App.__init__(self, redirect, filename)
def ...
1
vote
2answers
5k views
Python constructor does weird things with optional parameters [duplicate]
Possible Duplicate:
least astonishment in python: the mutable default argument
Can you help me understand of the behaviour and implications of the python __init__ constructor. It seems ...
3
votes
2answers
913 views
Python - configuration options, how to input/handle?
When your application takes a few (~ 5) configuration parameters, and the application is going
to be used by non-technology users (i.e. KISS), how do you usually handle reading
configuration options, ...
0
votes
2answers
920 views
optional arguments when calling a function without modifying function definition
I want to know how to call a function, passing a parameter that it might not be expecting.
I faced this problem a few days ago, and found a way around it. But today, I decided I'd see if what I ...