At the moment I have never had a problem with whitespace in Python (although I\'ve only used it in two projects and I was the only programmer). What are some potential pitf
If your using emacs, set a hard tab length of 8 and a soft tab length of 4. This way you will be alterted to any extraneous tab characters. You should always uses 4 spaces instead of tabs.
yeah there are some pitfalls, but most of the time, in practice, they turn out to be enemy windmills of the Quixotic style, i.e. imaginary, and nothing to worry about in reality.
I would estimate that the pitfalls one is most likely to encounter are (including mitigating steps identified):
working with others a.k.a. collaboration
a. if you have others which for whatever reason refuse to adhere to PEP 8, then it could become a pain to maintain code. I've never seen this in practice once I point out to them the almost universal convention for python is indent level == four spaces
b. get anyone/everyone you work with to accept the convention and have them figure out how to have their editor automatically do it (or better yet, if you use the same editor, show them how to configure it) such that copy-and-paste and stuff just works.
having to invest in a "decent" editor other than your current preferred one, if your current preferred editor is not python friendly -- not really a pitfall, more an investment requirement to avoid the other pitfalls mentioned associated with copy-and-paste, re-factoring, etc. stop using Notepad and you'll thank yourself in the morning.
a. your efficiency in editing the code will be much higher under an editor which understands python
b. most modern code editors handle python decently. I myself prefer GNU Emacs, and recent versions come with excellent python-mode
support out-of-the-box. The are plenty of other editors to explore, including many free alternatives and IDEs.
c. python itself comes out of the box with a "smart" python editor, idle
. Check it out if you are not familiar, as it is probably already available with your python install, and may even support python
better than your current editor. PyCrust is another option for a python editor implemented in python, and comes as part of wxPython.
some code generation or templating environments that incorporate python (think HTML generation or python CGI/WSGI apps) can have quirks
a. most of them, if they touch python, have taken steps to minimize the nature of python as an issue, but it still pops up once in a while.
b. if you encounter this, familiarize yourself with the steps that the framework authors have already taken to minimize the impact, and read their suggestions (and yes they will have some if it has ever been encountered in their project), and it will be simple to avoid the pitfalls related to python on this.
If you use Eclipse as your IDE, you should take a look at PyDev; it handles indentation and spacing automatically. You can copy-paste from mixed-spacing sources, and it will convert them for you. Since I started learning the language, I've never once had to think about spacing.
And it's really a non-issue; sane programmers indent anyway. With Python, you just do what you've always done, minus having to type and match braces.
That actually kept me away from Python for a while. Coming from a strong C background, I felt like I was driving without a seat belt.
It was aggravating when I was trying to fill up a snippet library in my editor with boilerplate, frequently used classes. I learn best by example, so I was grabbing as many interesting snippets as I could with the aim of writing a useful program while learning.
After I got in the habit of re-formatting everything that I borrowed, it wasn't so bad. But it still felt really awkward. I had to get used to a dynamically typed language PLUS indentation controlling my code.
It was quite a leap for me :)
I used to think that the white space issues was just a question of getting used to it.
Someone pointed out some serious flaws with Python indentation and I think they are quite valid and some subconcious understanding of these is what makes experienced programs nervious about the whole thing:-
Pick a good editor. You'd want features such as:
For example, Vim lets me highlight tabs with these settings:
set list
set listchars=tab:\|_
highlight SpecialKey ctermbg=Red guibg=Red
highlight SpecialKey ctermfg=White guifg=White
Which can be turned off at any time using:
set nolist
IMO, I dislike editor settings that convert tabs to spaces or vice versa, because you end up with a mix of tabs and spaces, which can be nasty.