Python Forum
Style question on adherence to PEP 8 with whitespace near an "=" sign
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Style question on adherence to PEP 8 with whitespace near an "=" sign
#1
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.
Reply
#2
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Do you agree that line 7 of the code you posted is in the "Wrong" section?
Reply
#4
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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
Reply
#6
Is this next line correct?

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

def munge(input: AnyStr, limit=1000): ...
Reply
#7
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Does @ at sign used for tother than decorators? ggpython000 1 536 Oct-12-2023, 09:08 AM
Last Post: buran
  Need to sign JWT token with JWK key stucoder 1 1,686 Feb-21-2022, 09:04 AM
Last Post: stucoder
  i making a terminal sign up website thing Kenrichppython 1 1,700 Nov-04-2021, 03:57 AM
Last Post: bowlofred
  Syntax Error with = sign and more Kathleen57 3 2,637 May-03-2020, 03:52 AM
Last Post: buran
  Superfluous whitespace found? CaptainCsaba 2 21,239 Mar-19-2020, 09:04 AM
Last Post: ak52
  Whitespace syntax error klp21 1 3,051 May-22-2019, 07:49 AM
Last Post: Gribouillis
  Handling pound sign (#) within custom URL chisox721 5 6,544 Apr-02-2019, 10:01 PM
Last Post: chisox721
  How to remove whitespace from a string when .replace and .strip do not work winnetrie 7 4,451 Jan-05-2019, 08:44 AM
Last Post: DeaD_EyE
  Sign XADES-EPES Python pedro86porras 2 4,232 Nov-02-2018, 04:31 PM
Last Post: pedro86porras
  Geany 1.25 terminal not showing whitespace hudabaig 1 3,000 May-14-2018, 10:50 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020