13

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.

Peque
  • 13,638
  • 11
  • 69
  • 105
  • 1
    Actually they've used spaces in that PEP https://www.python.org/dev/peps/pep-0484/#instantiating-generic-classes-and-type-erasure – vishes_shell May 11 '17 at 11:34
  • 1
    https://www.python.org/dev/peps/pep-3107/#syntax – Nemoden May 11 '17 at 11:38
  • @vishes_shell 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). – Peque May 11 '17 at 11:38

1 Answers1

13

The examples in PEP 484 all use

def foo(x: int = 1):
Nils Werner
  • 34,832
  • 7
  • 76
  • 98
  • 7
    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). – Peque May 11 '17 at 11:40