This question already has answers here:
Why do Python function docs include the comma after the bracket for optional args?
(5 answers)
Closed 6 years ago.
Since the first times I started to study Python I met many schematic codes such as
pickle.dump(obj, file[, protocol])
Now in this example I can understand the meaning of the first comma, as it separate two different arguments to be inserted in a method, but I don't understand the second comma that is located after a square bracket.
Is there anyone who can explain me the meaning of this comma?
it's a common notation for indicating the next argument is optional. so you could write:
pickle.dump(obj, file)
or you could write:
pickle.dump(obj, file, protocol)
if you see angle brackets like <foo>, that is used to indicate the argument is required.
Related
This question already has an answer here:
What does it mean in documentation when arguments are in square brackets? [duplicate]
(1 answer)
Closed 1 year ago.
What does the brackets around input arguments mean?
Consider this example:
cv.boxPoints(box[,points])
example doc link
What confuses me the most is the comma inside the bracket of the second input argument points. Why is it represented as [,points]. Any information on this would be greatly appreciated.
Thanks!
It's a common convention in documentation to denote optional arguments with square brackets. That documentation is telling you that the second argument to boxPoints is optional. It has no syntactic meaning in Python; you can call the function with one or two arguments like normal.
This question already has answers here:
Remove Last instance of a character and rest of a string
(5 answers)
Closed 3 years ago.
I have a string such as:
string="lcl|NC_011588.1_cds_YP_002321424.1_1"
and I would like to keep only: "YP_002321424.1"
So I tried :
string=re.sub(".*_cds_","",string)
string=re.sub("_\d","",string)
Does someone have an idea?
But the first _ is removed to
Note: The number can change (they are not fixed).
"Ordinary" split, as proposed in the other answer, is not enough,
because you also want to strip the trailing _1, so the part to capture
should end after a dot and digit.
Try the following pattern:
(?<=_cds_)\w+\.\d
For a working example see https://regex101.com/r/U2QsFH/1
Don't bother with regexes, a simple
string.split('_cds_')[1]
will be enough
This question already has answers here:
Python csv string to array
(10 answers)
In regex, match either the end of the string or a specific character
(2 answers)
Closed 3 years ago.
I need to capture words separated by tabs as illustrated in the image below.
The expression (.*?)[\t|\n] works well, except for the last line where a line feed is missing. Can anyone suggest a modification of the regular expression to also match the last word, i.e. Cheyenne? Link to code example
Replace [\t|\n] with (\t|$).
BTW, [\t|\n] is a character class, so the pipe | is literal here. You probably meant [\t\n].
This question already has answers here:
What do square brackets, "[]", mean in function/class documentation?
(4 answers)
Closed 3 months ago.
I have not understood why they write round(x[,n]) in syntax, but in codes they write round(10.6987,12) without square brackets before comma i.e, round(10.6987[,12])
The square brackets aren't intended to by typed into your code. They just indicate that n is an optional parameter.
This style is recommended in the Documenting Python guide:
function
Describes a module-level function. The signature should include the parameters, enclosing optional parameters in brackets. Default values can be given if it enhances clarity. For example:
Square brackets are just a convention used to indicate optional arguments.
This question already has answers here:
What is the syntax rule for having trailing commas in tuple definitions?
(10 answers)
Closed 10 years ago.
I am new to python and Django and am trying to determine how the following code works:
return HttpResponseRedirect(reverse('poll_results', args=(p.id,)))
Specifically the 2nd argument of the reverse function. It looks like it is setting the parameter of args equal to a tuple. Why do I need an extra comma?
(p.id) is just p.id in parentheses, (p.id,) is a single-element tuple.
Parenthesized forms in docs
The trailing comma is required if a tuple only has one item to differentiate a tuple from stylistic parenthesis.
Similar questions:
Python tuple comma syntax rule
Why does adding a trailing comma after a string make it a tuple