I have been doing some coding in Python recently. While playing around with some code, I noticed that the way heredocs are used in Python is different from Ruby.
Consider the following snippet:
# test.py print "some text in single line" print """ As opposed to some text written as heredoc """ print "and then another single line"
When run, this would result in this:
bash-3.2$ python test.py some text in single line As opposed to some text written as heredoc and then another single line
Notice how there is a preceding and trailing linebreak around the string printed using heredoc.
I spent a lot of time trying to figure out how and why this was happening. Finally, help came from
#python. Looks like I was doing it wrong. In Python, the useful part of the heredoc starts immediately after the
""". So if you don’t want those excess linebreaks, you should write something like this:
# test2.py print "some text in single line" print """As opposed to some text written as heredoc""" print "and then another single line"
When run, this would print the text without the extra line breaks:
bash-3.2$ python test2.py some text in single line As opposed to some text written as heredoc and then another single line
If you don’t want to write the string just after the
""", you could use a
\ to remove the linebreaks, like this:
# test3.py print """\ As opposed to some text written as heredoc\ """
I come from the Ruby world, where the heredocs behave in a slightly different way. The equivalent code in Ruby would look like this:
# test.rb puts "some text in single line" puts <<-STR As opposed to some text written as heredoc STR puts "and then another single line"
Notice how the actual content of the heredoc begins only on the line after
I hope being aware of this detail saves you some time.
If you have questions or comments about this blog post, you can get in touch with me on Twitter @sdqali.
If you liked this post, you'll also like...
- Implementing feature toggles for a Spring Boot application - Part 4
- Implementing feature toggles for a Spring Boot application - Part 3
- Implementing feature toggles for a Spring Boot application - Part 2
- Implementing feature toggles for a Spring Boot application - Part 1
- Setting up a secure etcd cluster behind a proxy
- Handling Deserialization errors in Spring Redis Sessions
- CSRF Protection with Spring Security and Angular JS
- Controlling Redis auto-configuration for Spring Boot Session
- JWT authentication with Spring Web - Part 5
- JWT authentication with Spring Web - Part 4
- JWT authentication with Spring Web - Part 3
- JWT authentication with Spring Web - Part 2
- JWT authentication with Spring Web - Part 1
- JSON logging for Spring applications
- Injecting dependencies into a Spring @Configuration
- Filtering responses in Spring MVC
- Deprecating domain events in Axon
- Programmable exit codes for spring command line applications - 2
- Programmable exit codes for Spring command line applications
- Using custom arguments in Spring MVC controllers
- Authentication for Apache Camel HTTP components
- Thoughts on Open Graph tags
- Integration testing Spring command line applications
- Integration testing challenges for non-web Spring applications
- How thinking of Documentation as Legislation helped me become a better programmer
- Implementing custom annotations for Spring MVC
- Validating RequestParams and PathVariables in Spring MVC
- Testing async responses using MockMvc
- Running multiple applications in the same Tomcat installation
- Making sense of Cloud Foundry security group declarations
- Configuring Cloud Foundry Java Memory Parameters
- Importing the Yelp dataset into MongoDB
- Clojure Dojo - Levenshtein edit distance
- A simple JMeter test with login
- Implementing Rate Limiting in Rails - Part 2
- Implementing Rate Limiting in Rails - Part 1
- Python Hack - Dynamically override an object's attribute
- Fitting an Image in to a Canvas object
- Accessing Environment Variables in Gradle
- Reading user input in Gradle scripts
- Ruby, Named Capture Groups and Local Variables
- Named Capture Groups in Regular Expressions
- Decomposing URLs in Python
- Shared history in Bash
- Managing Gemsets in Rbenv
- Looking up Compiler params used to compile a Ruby version
- Navigating Stacktraces in Emacs
- Python's bool type
- Graph databases 1 - Modeling
- Validating JSON in Emacs
- Emacs hack: Viewing Git logs while composing commit messages
- Configure Git's comment character
- My experience working remotely
- Oh I can build it in...
- Reducing Emacs startup time while committing
- My first Firefox plugin: GetCache - View cached version of the current page
- GetCache - A Chrome plugin to view cached version of the current page
- On REST, Content-Type, Google Chrome and Caching
- How Browsers Detect If You Are Offline
- D3.js Workshop
- Visualisation - How European clubs dominate their leagues
- Understanding Python's "with" statement
- Micro Journal - simple Git-backed journal in Python
- VodQA NCR: Maintaining Large Test Suites
- Know Your Tools - Don't Shoot Yourself in the Foot
- Managing security certificates from the console - on Windows, Mac OS X and Linux
- Indian and Pakistani cricketers - who make better debuts?
- Fixing Flyspell for Emacs in Mac OS X
- Finding un-merged commits with git cherry
- Bullet proof Jenkins setup
- Why your project should have a Getting Started guide.
- Debugging: C Sharp's HttpWebRequest, 100-Continue and nginx
- Wikipedia Page Hopping
- Empathy Log Parser
- Binary Signature Art
- Java Arrays in JRuby
- Autorun.py - Execute stuff on file change