Python with statement break line without backslash [duplicate] - python

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:

Related

How to break a long python line (assignment) with very long variable names [duplicate]

This question already has answers here:
How can I do a line break (line continuation) in Python?
(10 answers)
Closed 10 months ago.
This is not my code, but I'm asked to fix some flake8 lint errors.
There's a line in the code that looks like this
some_very_longggg_variable_name_on_the_lhs = SomeLongClassNameIsHere.func_name(input_dt=some_default_input_dt_that_has_a_really_long_name_here)
I'm unable to change the names of the variables. The number of columns is 144 and I need to get it to 120.
It doesn't allow me to break it at the equal sign like the following and I also tried putting the argument of func_name on the next line, but flake8 requires aligning the new line with the ( on the previous line so that doesn't help. I'm not sure what else to do.
some_very_longggg_variable_name_on_the_lhs
= SomeLongClassNameIsHere.func_name(input_dt=some_default_input_dt_that_has_a_really_long_name_here)
Python will not break a statement inside parentheses of any kind, so
a = (
b
)
is the simplest, most legible and most maintainable way to do this.

Why semi-colon in python after end of print and variable working? [duplicate]

This question already has answers here:
What does a semicolon do?
(5 answers)
Closed 5 years ago.
I am new to python and currently working in it.
I have write one simple program, as i read somewhere that semicolon not allowed or required in python to end statement. but i have use that and till its working fine! anyone explain me
why its possible?
here is code.
a = 10;
if a == 10:
print "value of a is %s"%(a);
else:
print "value of a is not %s"%(a);
semicolon is allowed as statement separator
>>> a=1;b=2;c=3
>>> print(a,b,c)
1 2 3
It is not required if you write each statement in a new line
Python does not require semi-colons to terminate statements. Semi-colons can be used to delimit statements if you wish to put multiple statements on the same line.

How to break long lines in python that have no brackets or operators? [duplicate]

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)
)

Why import statements with parentheses? [duplicate]

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.

What does the ' ; ' symbol mean in python? [duplicate]

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.

Categories