Bare asterisk in function arguments?

What does a bare asterisk in the arguments 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 arguments (for variable number of arguments), but this precedes nothing. And I’m pretty sure this has nothing to do … Read more

Expanding tuples into arguments

Is there a way to expand a Python tuple into a function – as actual parameters? For example, here expand() does the magic: some_tuple = (1, “foo”, “bar”) def myfun(number, str1, str2): return (number * 2, str1 + str2, str2 + str1) myfun(expand(some_tuple)) # (2, “foobar”, “barfoo”) I know one could define myfun as myfun((a, … Read more

How do I pass multiple parameters into a function in PowerShell?

If I have a function which accepts more than one string parameter, the first parameter seems to get all the data assigned to it, and remaining parameters are passed in as empty. A quick test script: Function Test([string]$arg1, [string]$arg2) { Write-Host “`$arg1 value: $arg1” Write-Host “`$arg2 value: $arg2” } Test(“ABC”, “DEF”) The output generated is … Read more

How can I pass an argument to a PowerShell script?

There’s a PowerShell script named itunesForward.ps1 that makes iTunes fast forward 30 seconds: $iTunes = New-Object -ComObject iTunes.Application if ($iTunes.playerstate -eq 1) { $iTunes.PlayerPosition = $iTunes.PlayerPosition + 30 } It is executed with a prompt line command: powershell.exe itunesForward.ps1 Is it possible to pass an argument from the command line and have it applied in … Read more

What does map(&:name) mean in Ruby?

I found this code in a RailsCast: def tag_names @tag_names || tags.map(&:name).join(‘ ‘) end What does the (&:name) in map(&:name) mean? 16 s 16 It’s shorthand for tags.map(&:name.to_proc).join(‘ ‘) If foo is an object with a to_proc method, then you can pass it to a method as &foo, which will call foo.to_proc and use that … Read more

What does the star and doublestar operator mean in a function call?

What does the * operator mean in Python, such as in code like zip(*x) or f(**k)? How is it handled internally in the interpreter? Does it affect performance at all? Is it fast or slow? When is it useful and when is it not? Should it be used in a function declaration or in a … Read more

How to pass all arguments passed to my bash script to a function of mine? [duplicate]

This question already has answers here: Propagate all arguments in a bash shell script (12 answers) Closed 2 years ago. Let’s say I have defined a function abc() that will handle the logic related to analyzing the arguments passed to my script. How can I pass all arguments my bash script has received to it? … Read more

Check number of arguments passed to a Bash script

I would like my Bash script to print an error message if the required argument count is not met. I tried the following code: #!/bin/bash echo Script name: $0 echo $# arguments if [$# -ne 1]; then echo “illegal number of parameters” fi For some unknown reason I’ve got the following error: test: line 4: … Read more