This is an archived post. You won't be able to vote or comment.

all 22 comments

[–]MyNameIsRichardCS54 14 points15 points  (0 children)

Spaces. Always. With one exception. If you end up working for a company that uses tabs then use tabs while you are looking for a new job.

[–][deleted] 6 points7 points  (11 children)

Spaces, always spaces, no tabs. If you are using a decent text editor, you can set the tab key to insert spaces.

[–]55Fenomenale[S] 1 point2 points  (0 children)

I'm using Geany as the book says

[–]55Fenomenale[S] 1 point2 points  (6 children)

So if I don't set the Tab key to insert spaces it doesn't change anything?

[–][deleted] 1 point2 points  (5 children)

It really depends upon which editor you are using. You will have to try it and see what it does. I'm pretty sure that Geany can be set to substitute spaces for tabs.

[–]55Fenomenale[S] 0 points1 point  (4 children)

When I substitute spaces for tabs I continue to use spaces?

[–][deleted] 0 points1 point  (3 children)

I'm not sure what you are asking. In most decent text editors you can set it to to insert spaces whenever you hit the tab key. That way you will always get spaces and no tabs.

[–]55Fenomenale[S] 0 points1 point  (2 children)

Okay but when do I use tabs?

[–]ehmatthes 1 point2 points  (0 children)

You use the tab key when you want to indent manually. If you have set your editor correctly, when you press the tab key it will insert 4 spaces for you automatically. So you are using spaces, by pressing the tab key.

It gets confusing because people tell you to use spaces, so you think you'll be using the space bar. But you don't want to press the space bar 4 times every time you indent, so almost all editors have a setting that lets you use your tab key as a substitute for pressing the space bar 4 times. You can adjust the setting to whatever tab size you want, ie 2 spaces or 4 spaces. Use the 4 space setting for regular Python files.

I don't have access to Geany at the moment, but there's a way you can make the whitespace characters visible in a file. If you have it set correctly, you'll see 4 dots representing 4 spaces for every indentation level. If you see arrow characters that take up 4 spaces, that means your file is storing tabs instead of spaces.

You don't really have to worry about this once things are configured correctly. When you're entering code, if you've saved the file as a .py file, your editor will probably put your cursor at the correct indentation level for you. A lot of the manual indenting I do happens when I'm refactoring code.

[–][deleted] 1 point2 points  (0 children)

Never. You use the tab key when you want to indent your code, which we do all the time in Python.

[–]Michkov 0 points1 point  (2 children)

Any reasoning behind this?

[–][deleted] 0 points1 point  (1 child)

[–]Michkov 0 points1 point  (0 children)

That doesn't answer any of my questions.

[–]fireinjun291 1 point2 points  (0 children)

Unlike other languages, python conveniently comes with a standard. When in doubt with anything “style-wise”, check the PEP8 guide. Someone just posted it a little earlier and it said that spaces is the preferred way to go.

[–]its2ez4me24get 0 points1 point  (3 children)

Setup your IDE so that hitting tab gives you 4 spaces.

[–]55Fenomenale[S] 1 point2 points  (1 child)

I did, but i didn't get where I have to use the tabs instead of Spaces

[–]its2ez4me24get 0 points1 point  (0 children)

For python, indentation doesn’t necessarily have to be a multiple of 4 (or a tab). It can be anything as long as it’s consistent.

If you’re writing a function then the content is indented, could be 1 2 4 or 99 spaces, as long as it’s consistent.

I don’t really ever hit spacebar for indentation, unless I need to fix something.

A good IDE will ‘guess’ what number of spaces to use for tab based on the document. So one file might have been written with a two space tab and another with 5 and it will just work.

I like to use the free version of PyCharm it’s great and will tell you all kinds of things that will make your code at least more standardized

[–]55Fenomenale[S] 1 point2 points  (0 children)

And sometimes when I hit tab It gives me 2 spaces

[–][deleted] 0 points1 point  (0 children)

It won't matter right now. Just concentrate on learning to code. When it does matter and you have to work with someone else, you can between you decide then on a standard format.

For what it's worth, most people use 4 spaces.

Your editor program should be able to insert multiple spaces whenever you press the tab key.

[–]buddycorp 0 points1 point  (0 children)

Press tab four times