Python Forum

Full Version: Style question on adherence to PEP 8 with whitespace near an "=" sign
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Python.org (in the PEP 8 Style Guide for Python Code) says that this is incorrect syntax:

def munge(input: AnyStr, limit = 1000): ...
But to me this uses spaces around a default value assignment correctly. This is consistent with PEP 8's "Other Recommendations": https://www.python.org/dev/peps/pep-0008...mendations

Would "limit=1000" be correct? I wouldn't think so because this section seems unannotated. If it is annotated, it is combining a default value with the annotation (namely "input"). In either of these cases, I would think a space around the equals sign "=" would be necessary.
(Jan-12-2021, 07:39 PM)nilesh Wrote: [ -> ]Python.org (in the PEP 8 Style Guide for Python Code) says that this is incorrect syntax:
Where did it say its incorrect? I was not able to find it. Both times you refer to PEP8.

You are right that it is consistent with the recommendations in PEP8:


Quote:When combining an argument annotation with a default value, however, do use spaces around the = sign:

# Correct:
def munge(sep: AnyStr = None): ...
def munge(input: AnyStr, sep: AnyStr = None, limit=1000): ...

# Wrong:
def munge(input: AnyStr=None): ...
def munge(input: AnyStr, limit = 1000): ...

and also
Quote:Don't use spaces around the = sign when used to indicate a keyword argument, or when used to indicate a default value for an unannotated function parameter:

EDIT: I scratched my initial response, because I overlooked the example from OP post.
Do you agree that line 7 of the code you posted is in the "Wrong" section?
(Jan-12-2021, 08:08 PM)nilesh Wrote: [ -> ]Do you agree that line 7 of the code you posted is in the "Wrong" section?

yes, it's in the # wrong section. there should NOT be space around = for limit=1000
The is a difference when type annotation is used.
other-recommendations
pep8 Wrote:When combining an argument annotation with a default value,
use spaces around the = sign (but only for those arguments that have both an annotation and a default).

Yes:

def munge(sep: AnyStr = None): ...

No:

def munge(input: AnyStr=None): ...
def munge(input: AnyStr, limit = 1000): ...

So if look these function that do same.
So this is the recommended way,with type annotation and without.
def func(num1: int, my_float: float = 3.5) -> float:
    return num1 + my_float

def func1(num1, my_float=3.5):
    return num1 + my_float
I think i would have written it like this,old habit i think it look better.
No used type annotation so much yet
def func(num1: int, my_float: float=3.5) -> float:
    return num1 + my_float
Is this next line correct?

def munge(input: AnyStr, limit = 1000): ...
Is this next line correct?

def munge(input: AnyStr, limit=1000): ...
This is correct.
def munge(input: AnyStr, limit=1000): ...
I see there where a error in my last other post.
So should be like this.
def func(num1: int, my_float, float=3.5) -> float:
    return num1 + my_float
If unsure run black or paste into Black Playground.