parameter-passing

Function restriction by fixing an argument

Function restriction by fixing an argument Question: How should I make function with lesser dimensionality than the original one by fixing an argument of it: For example I want to make successor function out of sum function as follows: def add(x, y): return x + y Now I am looking for something like this: g …

Total answers: 2

Bare asterisk in function arguments?

Bare asterisk in function parameters? Question: What does a bare asterisk in the parameters of a function do? When I looked at the pickle module, I see this: pickle.dump(obj, file, protocol=None, *, fix_imports=True) I know about a single and double asterisks preceding parameters (for variable number of parameters), but this precedes nothing. And I’m pretty …

Total answers: 6

Problem with reading in parameters with special characters in Python

Problem with reading in parameters with special characters in Python Question: I have a scripts (a.py) reads in 2 parameters like this:- #!/usr/bin/env python import sys username = sys.argv[1] password = sys.argv[2] Problem is, when I call the script with some special characters:- a.py "Lionel" "my*password" It gives me this error:- /swdev/tools/python/current/linux64/bin/python: No match. Any …

Total answers: 3

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

How to pass arguments to the __code__ of a function?

How to pass arguments to the __code__ of a function? Question: The following works: def spam(): print “spam” exec(spam.__code__) spam But what if spam takes arguments? def spam(eggs): print “spam and”, eggs exec(spam.__code__) TypeError: spam() takes exactly 1 argument (0 given) Given, that I don’t have access to the function itself but only to a …

Total answers: 6

Passing an array/list into a Python function

Passing an array/list into a Python function Question: I’ve been looking at passing arrays, or lists, as Python tends to call them, into a function. I read something about using *args, such as: def someFunc(*args) for x in args print x But not sure if this is right/wrong. Nothing seems to work as I want. …

Total answers: 5

Pass a list to a function to act as multiple arguments

Pass a list to a function to act as multiple arguments Question: In a function that expects a list of items, how can I pass a Python list item without getting an error? my_list = [‘red’, ‘blue’, ‘orange’] function_that_needs_strings(‘red’, ‘blue’, ‘orange’) # works! function_that_needs_strings(my_list) # breaks! Surely there must be a way to expand the …

Total answers: 3

Django return redirect() with parameters

Django return redirect() with parameters Question: In my view function I want to call another view and pass data to it : return redirect(‘some-view-name’, backend, form.cleaned_data) , where backend is of registration.backends object, and form.cleaned_data is a dict of form data (but both must be either sent as *args or **kwargs to prevent raising Don’t …

Total answers: 5

Forced naming of parameters in Python

Forced naming of parameters in Python Question: In Python you may have a function definition: def info(object, spacing=10, collapse=1) which could be called in any of the following ways: info(odbchelper) info(odbchelper, 12) info(odbchelper, collapse=0) info(spacing=15, object=odbchelper) thanks to Python’s allowing of any-order arguments, so long as they’re named. The problem we’re having is as some …

Total answers: 11