Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blank line is not allowed in python functions? [duplicate]

Tags:

python

syntax

(A) This works:

def func():
    x = 1
    print(x)    
    return

(B) This doesn't work:

def func():
    x = 1

    print(x)    
    return

(C) This also doesn't work: (Here I am using '_' to indicate a white space)

def func():
    x = 1
____
    print(x)    
    return

(D) This works:

def func():
    x = 1
____# some comment
    print(x)    
    return

In both (B) and (C) I get the following errors:

  • NameError: name 'x' is not defined
  • SyntaxError: 'return' outside function

I am using Eclipse and the PyDev plug-in. The Python version is 3.5 and the PyDev version is 4.5.

----------update----------

  1. The issue is not reproducible in Jupyter Notebook and PyCharm.

  2. In Eclipse I have turned on "Show whitespace characters". The indent in (C) is indeed composed of 4 whitespaces which are displayed as 4 dots in the editor. If it was a tab, in the Eclipse editor it would be displayed as ">> ".

Given my through investigation and the comments/answers below, I am pretty sure this is a stupid bug of Eclipse and/or PyDev.

Thank you all for helping.

like image 993
GoCurry Avatar asked Apr 12 '26 15:04

GoCurry


1 Answers

be careful with tabs and spaces, they are not the same, so:

(_ are withe spaces)

def func():
____x = 1
____
____print(x)
____return

It will work just fine

is not the same as

(+ are tabs, _ white spaces)

def func():
++x = 1
__
++print(x)
++return

Won't work

like image 111
A Monad is a Monoid Avatar answered Apr 15 '26 05:04

A Monad is a Monoid