Type annotation style (to space or not to space)
Asked Answered
H

1

15

Having the following function:

def foo(x=1):
    print(x)

It is clearly stated in PEP 8 that no spaces should be used around the = sign when used to indicate a keyword argument or a default parameter value.

If we want to type-annotate the x parameter. How should we do it?

def foo(x:int=1):
def foo(x: int=1):
def foo(x: int = 1):

Is there a preferred way? Or even better, is it specified in some PEP? Did not find it in PEP 484.

Homoousian answered 11/5, 2017 at 11:31 Comment(3)
Actually they've used spaces in that PEP python.org/dev/peps/pep-0484/…Fi
python.org/dev/peps/pep-3107/#syntaxStogy
@Fi You are right, thanks. I just searched for the keyword "space" in that PEP and did not find anything (i.e.: no actual rule apart from the examples).Homoousian
B
15

The examples in PEP 484 all use

def foo(x: int = 1):
Beechnut answered 11/5, 2017 at 11:34 Comment(1)
Thanks for your answer. It just seemed to be a bit weird to have all those spaces when in PEP 8 they are discouraged. Also, it seems there is no explicit rule/recommendation in PEP 484 about that (only the code examples).Homoousian

© 2022 - 2024 — McMap. All rights reserved.