indentation

Why does Python pep-8 strongly recommend spaces over tabs for indentation?

假如想象 提交于 2019-11-26 05:54:23
问题 I see on Stack Overflow and PEP 8 that the recommendation is to use spaces only for indentation in Python programs. I can understand the need for consistent indentation and I have felt that pain. Is there an underlying reason for spaces to be preferred? I would have thought that tabs were far easier to work with. 回答1: The answer was given right there in the PEP [ed: this passage has been edited out in 2013]. I quote: The most popular way of indenting Python is with spaces only. What other

How to implement custom indentation when pretty-printing with the JSON module?

荒凉一梦 提交于 2019-11-26 03:58:57
问题 So I\'m using Python 2.7, using the json module to encode the following data structure: \'layer1\': { \'layer2\': { \'layer3_1\': [ long_list_of_stuff ], \'layer3_2\': \'string\' } } My problem is that I\'m printing everything out using pretty printing, as follows: json.dumps(data_structure, indent=2) Which is great, except I want to indent it all, except for the content in \"layer3_1\" — It\'s a massive dictionary listing coordinates, and as such, having a single value set on each one makes

Changing Vim indentation behavior by file type

放肆的年华 提交于 2019-11-26 03:46:16
问题 Could someone explain to me in simple terms the easiest way to change the indentation behavior of Vim based on the file type? For instance, if I open a Python file it should indent with 2 spaces, but if I open a Powershell script it should use 4 spaces. 回答1: You can add .vim files to be executed whenever vim switches to a particular filetype. For example, I have a file ~/.vim/after/ftplugin/html.vim with this contents: setlocal shiftwidth=2 setlocal tabstop=2 Which causes vim to use tabs with

Tabs versus spaces in Python programming

帅比萌擦擦* 提交于 2019-11-26 02:26:42
问题 I have always used tabs for indentation when I do Python programming. But then I came across a question here on SO where someone pointed out that most Python programmers use spaces instead of tabs to minimize editor-to-editor mistakes. How does that make a difference? Are there other reasons why one would use spaces instead of tabs for Python? Or is it simply not true? Should I switch my editor to insert spaces instead of tabs right away or keep on going like I used to? 回答1: Because PEP-8

How do I reformat HTML code using Sublime Text 2?

大兔子大兔子 提交于 2019-11-26 02:26:00
问题 I\'ve got some poorly-formatted HTML code that I\'d like to reformat. Is there a command that will automatically reformat HTML code in Sublime Text 2 so it looks better and is easier to read? 回答1: You don't need any plugins to do this. Just select all lines ( Ctrl A ) and then from the menu select Edit → Line → Reindent. This will work if your file is saved with an extension that contains HTML like .html or .php . If you do this often, you may find this key mapping useful: { "keys": ["ctrl

How to properly indent PHP/HTML mixed code? [closed]

白昼怎懂夜的黑 提交于 2019-11-26 02:09:28
问题 When mixing PHP and HTML, what is the proper indentation style to use? Do I indent so that the outputted HTML has correct indentation, or so that the PHP/HTML mix looks properly formatted (and is thus easier to read)? For example, say I have a foreach loop outputting table rows. Which one below is correct? PHP/HTML mix looks correct: <table> <?php foreach ($rows as $row): ?> <tr> <?php if ($row->foo()): ?> <?php echo $row ?> <?php else: ?> Something else <?php endif ?> </tr> <?php endforeach

Convert tabs to spaces in Notepad++

旧街凉风 提交于 2019-11-26 01:48:04
问题 How do I convert tabs to spaces in Notepad++? I found a webpage that suggests it\'s possible, but I couldn\'t find any information about how to do it. I would like to be able to do that, because some web forms don\'t respect code with tabs in them. 回答1: To convert existing tabs to spaces, press Edit->Blank Operations->TAB to Space . If in the future you want to enter spaces instead of tab when you press tab key: Go to Settings->Preferences...->Language (since version 7.1) or Settings-

Convert tabs to spaces in Notepad++

笑着哭i 提交于 2019-11-25 23:07:58
How do I convert tabs to spaces in Notepad++? I found a webpage that suggests it's possible, but I couldn't find any information about how to do it. I would like to be able to do that, because some web forms don't respect code with tabs in them. mrzli To convert existing tabs to spaces, press Edit->Blank Operations->TAB to Space . If in the future you want to enter spaces instead of tab when you press tab key: Go to Settings->Preferences...->Language (since version 7.1) or Settings->Preferences...->Tab Settings (previous versions) Check Replace by space ( Optional ) You can set the number of

I&#39;m getting an IndentationError. How do I fix it?

痞子三分冷 提交于 2019-11-25 22:20:58
问题 I have a Python script: if True: if False: print(\'foo\') print(\'bar\') However, when I attempt to run my script, Python raises an IndentationError : File \"script.py\", line 4 print(\'bar\') ^ IndentationError: unindent does not match any outer indentation level I kept playing around with my program, and I was also able to produce three other errors: IndentationError: unexpected indent IndentationError: expected an indented block TabError: inconsistent use of tabs and spaces in indentation

IndentationError: unindent does not match any outer indentation level

泪湿孤枕 提交于 2019-11-25 22:19:12
问题 When I compile the Python code below, I get IndentationError: unindent does not match any outer indentation level import sys def Factorial(n): # Return factorial result = 1 for i in range (1,n): result = result * i print \"factorial is \",result return result Why? 回答1: Other posters are probably correct...there might be spaces mixed in with your tabs. Try doing a search & replace to replace all tabs with a few spaces. Try this: import sys def Factorial(n): # return factorial result = 1 for i