This question already has answers here:
Is it possible to break a long line to multiple lines in Python? [duplicate]
(7 answers)
Closed 7 years ago.
Lately have seen imports like this
from module import (function, another_function,
another_function)
Seemingly this has been done to be able to stretch the import statement over more than one line. In cases like this I usually just import like so
from module import function, another_function, \
another_function
What exactly are the parentheses doing in this case and are they considered to be bad practice?
As PEP 8 states:
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.
Related
This question already has answers here:
Convert regular Python string to raw string
(12 answers)
Closed 1 year ago.
I wrote a function in Python that takes a file path as an argument. Ideally, I would like to 'concatenate' an r at the beginning to escape the characters, and turn it into r"C:\User\name\location".
I am having trouble finding any solutions- are there any modules to help with this?
You do not require any modifications to the function at all.
def f(path):
...
...
f(r"C:\User\name\location")
The "r" you referred to would be used to form the string that you pass to the function. A string is a string, it does not matter how you form it, but Python offers you different ways of doing so e.g.:
f("C:\\User\\name\\location")
By the time the function is passed the string, the string has already been formed. It now makes no difference how it was formed, only that it has all of the correct characters in all the correct places!
This question already has answers here:
How to write string literals in Python without having to escape them?
(6 answers)
Closed 5 years ago.
In F# there is something called a literal string (not string literal), basically if a string literal is preceded by # then it is interpreted as-is, without any escapes.
For example if you want to write the path of a file in Windows(for an os.walk for example) you would do it like this:
"d:\\projects\\re\\p1\\v1\\pjName\\log\\"
Or you could do this(the F# way):
#"d:\projects\re\p1\v1\pjName\log\"
The second variant looks much more clear and pleasing to the eye. Is there something of the sort in python? The documentation doesn't seem to have anything regarding that.
I am working in Python 3.6.3.
There are: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
You can use r prefix.
https://docs.python.org/2.0/ref/strings.html
TL;DR use little r
myString = r'\n'
This question already has answers here:
Python multi-line with statement
(6 answers)
Closed 5 years ago.
How can I break this line without using "\"?
with mock.patch('six.moves.builtins.open', mock.mock_open()), mock.patch('my_module.yaml.safe_load') as mock_yaml:
#do something
I tried with parenthesis but it complains with SyntaxError about the "as"
with (mock.patch('six.moves.builtins.open', mock.mock_open()),
mock.patch('my_module.yaml.safe_load') as mock_yaml):
#do something
Breaking it inside a set of parentheses, but without the parentheses around mock.patch(....), should work according to the PEP8 Python Style Guide, due to implied line continuation between parentheses:
with mock.patch('six.moves.builtins.open',
mock.mock_open()), mock.patch('my_module.yaml.safe_load') as mock_yaml:
The other option is similar to your second suggestion, but with the closing parentheses moved to before the as:
with (mock.patch('six.moves.builtins.open', mock.mock_open()),
mock.patch('my_module.yaml.safe_load')) as mock_yaml:
This question already has answers here:
How can I do a line break (line continuation) in Python?
(10 answers)
Closed 6 years ago.
This is different from similar questions I have found on this site as the code in question doesn't have any operators or brackets before the character limit to split easily on. I have several long lines in python without operators or brackets before 79 characters. As an example:
self.caller.parent.parent.parent.caller.parent.bar.ids.actionview.remove_widget(self.caller.parent.parent.parent.caller.parent.bar.ids.actionview.startbutton)
The above line has 72 characters before a bracket, and is within a function definition within a class so therefore with 4-char spacing per nest level, has 81 characters before a bracket.
What is the preferred way of dealing with this?
With your particular example, I expect if you first solved the problem of "make this code readable", fitting in 79 characters would come naturally.
That said, you can add brackets:
(self.caller.parent.parent
.parent.caller.parent.bar
.ids.actionview.remove_widget(
self.caller.parent.parent
.parent.caller.parent
.bar.ids.actionview.startbutton)
)
This question already has answers here:
Why is semicolon allowed in this Python snippet?
(15 answers)
What does a semicolon do?
(5 answers)
Closed 8 years ago.
I sometimes see this ; symbol on tutorials and such, what does it indicate?
Semi-colons are used in Python to separate statements in the same line.
print 1; print 2
Common use example:
import pdb; pdb.set_trace()
The semicolon can be used as a statement separator in Python, called stmt_list in the language reference, but its use is generally discouraged (PEP 8, compound statements). We prefer one statement per line.