indentation

Where is `filetype plugin` defined and how to override it?

Where is `filetype plugin` defined and how to override it? Question: I am trying set python indentation to use 2 spaces instead of 4 (or 8) spaces. I put the following in my ~/.vimrc: set expandtab set shiftwidth=2 set softtabstop=2 set tabstop=2 This works well, as long as I do not put filetype plugin on …

Total answers: 2

Indent items of YAML lists when dumping in Python

Indent items of YAML lists when dumping Question: The context Consider this minimal working example: I have this JSON file $ cat main.json [ { "name": "a", "numbers": [1, 2] }, { "name": "b", "numbers": [10, 20] } ] I need to convert that JSON file to YAML. The following script accomplishes that $ cat …

Total answers: 1

Change indentation level in Google Colab

Change indentation level in Google Colab Question: I am using Google Colab to write Python code in their notebooks. Whenever I hit enter after a loop or conditional, the new line is automatically indented, which is good, but it uses only 2 whitespaces by default. This is contrary to the PEP-8 standard which recommends 4 …

Total answers: 1

Indentation error on Visual Studio Code on a Mac (again)

Indentation error on Visual Studio Code on a Mac (again) Question: I am a newbie trying to use Python (2.17.15 via Anaconda) on Visual Stodio Code on my Mac. I have the following simple code: def function(x): y = x + 2 return y This code is giving me the usual trouble, an indentation error: …

Total answers: 3

inconsistent use of tabs and spaces in indentation notepad++ Python

inconsistent use of tabs and spaces in indentation notepad++ Python Question: I am getting an inconsistent use of tabs and spaces in indentation error after adding only one line of code and I can’t see why it would throw the error, here is the code: auth = tweepy.OAuthHandler(consumer_key, consumer_secret) #create authentcation handler auth.set_access_token(access_token, access_secret) #set …

Total answers: 2

IndentationError: unexpected indent after comment

IndentationError: unexpected indent after comment Question: I am trying to write some Python example code with a line commented out: user_by_email = session.query(User) .filter(Address.email==’one’) #.options(joinedload(User.addresses)) .first() I also tried: user_by_email = session.query(User) .filter(Address.email==’one’) # .options(joinedload(User.addresses)) .first() But I get IndentationError: unexpected indent. If I remove the commented out line, the code works. I am decently …

Total answers: 3

I'm getting an IndentationError. How do I fix it?

I'm getting an IndentationError. How do I fix it? Question: 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, …

Total answers: 6

Can a line of Python code know its indentation nesting level?

Can a line of Python code know its indentation nesting level? Question: From something like this: print(get_indentation_level()) print(get_indentation_level()) print(get_indentation_level()) I would like to get something like this: 1 2 3 Can the code read itself in this way? All I want is the output from the more nested parts of the code to be more …

Total answers: 5

Visual Studio Code indentation for Python

Visual Studio Code indentation for Python Question: How do I enable indentation in Visual Studio Code? I’m trying to learn Python (new to programming) and need auto-indentation. It worked with the first version I tried, but it doesn’t indent after a colon (:) any more. How can I configure it to automatically indent? Asked By: …

Total answers: 8

Conditional with statement in Python

Conditional with statement in Python Question: Is there a way to begin a block of code with a with statement, but conditionally? Something like: if needs_with(): with get_stuff() as gs: # do nearly the same large block of stuff, # involving gs or not, depending on needs_with() To clarify, one scenario would have a block …

Total answers: 9