Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spaces vs tabs #2888

Closed
vgoklani opened this issue Feb 6, 2013 · 10 comments
Closed

spaces vs tabs #2888

vgoklani opened this issue Feb 6, 2013 · 10 comments

Comments

@vgoklani
Copy link

vgoklani commented Feb 6, 2013

Hi,

I have a small issue when using the ipython notebook in the browser. I'm one of those people that likes to press tab, and unfortunately there are several instances when writing a for loop and incrementing a line, that the tabs get converted to spaces. And the end result is that I get an "IndentationError: unexpected indent". The solution is to move the code into sublime (or your text editor of choice), fix the tab/space issue, and then paste back into the notebook. Is there a workaround, surely I can't be the only person with this problem.

@Carreau
Copy link
Member

Carreau commented Feb 6, 2013

Right now, there are no easy way to change the behavior.
Depending on how much you want to dive in the code, the possibility are :

  • Having visible tabs (to distinguish tab/space)
  • indenting with tab instead of space
  • filter the cell to convert tab <-> space before sending to kernel.

The first 2 are just one line codemirror options that have to be added in the right place.

Just to be sure, when using the notebook, you get tabs in the codecell, right ?
How does that happend ? When you press tab ? Or copy and past ? Because IIRC tabkey should never insert tab characters in notebook, but should add/remove space until the next multiple of 4.

Or indent/dedent is something is selected, or trigger tooltip or completion, but this is a detail.

@damianavila
Copy link
Member

In current master, if you paste a code containing tabs, there is a visual indicator (arrow) to show you where the tabs are located
And if you press TAB inside the notebook at the beginning of a line, it is automatically converted to 4 spaces... so, I think we can close this one.

@minrk minrk closed this as completed Feb 2, 2014
@billgale
Copy link

Python either allows tabs or spaces for indentation. I prefer to use tabs. When I cut and paste tab-indented code into iPython, how can I get the tab key on the keyboard to insert a tab character into a cell? It appears by default with iPython that pressing tab on your keyboard inserts spaces into the cell. Then when you execute the cell this causes Python to complain about indentation. In the example below print statement was added in iPython and has space indentation while the other lines were copied from a pyCharm project with tab indentation.

gpstime_sec = gpstimesec_from_gpstimesow(gpsweek, gpssecofweek, verbose=verbose)
print 'gpstime_sec: {}'.format(gpstime_sec)
return

File "", line 10
print 'gpstime_sec: {}'.format(gpstime_sec)
^
IndentationError: unindent does not match any outer indentation level

I think this is what vgoklani was trying to ask here.

@vgoklani
Copy link
Author

indeed :)

@billgale
Copy link

I seem to be running into you a lot now. Is this you referenced in Phillip Cloud’s reply?

http://stackoverflow.com/questions/10729210/iterating-row-by-row-through-a-pandas-dataframe

From: Vishal Goklani [mailto:notifications@github.com]
Sent: Wednesday, April 15, 2015 2:38 PM
To: ipython/ipython
Cc: Gale, Bill
Subject: Re: [ipython] spaces vs tabs (#2888)

indeed :)


Reply to this email directly or view it on GitHubhttps://github.com//issues/2888#issuecomment-93542561.

@vgoklani
Copy link
Author

yup!

@keldLundgaard
Copy link

Any news on this? I too would like to use tabs in the cells of my Jupyter notebooks so that I do not need to change the code between my editor and my notebooks.

@carlsmith
Copy link
Contributor

carlsmith commented Apr 3, 2017

The Python Style Guide says don't use tabs. Same for Ruby, PHP and Java, as well as Google's style guides for CSS and C++. I just picked Python and the first five others I could find.

A lot of effort has already been made to phase tabs out entirely, because they're problematic and don't do anything that can't easily be achieved with spaces. Tabs are only supported at all because old code contains them. New languages (and new filetypes for existing languages) should not allow tabs (except when using \t inside of string literals).

Python either allows tabs or spaces for indentation.

It'd be more accurate to say that Python tolerates tabs, but strongly recommends using spaces.

@daseibert
Copy link

Here is a solution I found on stackoverflow: https://stackoverflow.com/questions/40861769/in-jupyter-notebook-how-to-change-auto-indent-to-tab-instead-of-4-spaces

It's kind of amusing how some people can't seem to stop themselves from making preachy, unhelpful comments when these topics come up.

@johndavidmiller
Copy link

to each their own, but it is def a PITA that different editors, e.g., vi vs. jupyter, handle this differently (at least with my settings; it's not their fault) and then it later cause my Python code to barf. if I were the king of Python I'd do something a little more intelligent there, like figuring out that when someone TABed they meant the same as when someone (invariably with carpal thumbal syndrome) typed 4 spaces. i.e., whitespace. that's what a human would do, right? well, maybe not you high school language teacher, but reasonable humans would see that and say, oh, i get it.

personally, I don't understand why anyone would prefer to type exactly 4 spaces when they really mean 1 unit of indention, aka TAB. esp. with a language like Python what will barf if you accidentally type 3 or 5. that just seems unnecessarily complicated to me. hit the damn TAB key and get on with it.

all that said, as a sw eng with 40 years experience in zillions of languages, notably C/C++, it is really jarring to back to a non-Python (or Scala) language where I have to add semicolons and curly braces. how quaint.

(personally, I think Smalltalk had the best readability, but it is an acquired taste, like Lisp.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants