Background
- Python (version 2.7.2)
Problem
- You want to specify an array in your code.
- You want to use the 'syntax sugar' available in Ruby and Perl.
- You are a fanatic about readable code but also saving keystrokes.
Solution
- Python Triple-quoted string
- Python splitlines
- Python list comprehension
Pitfalls
- The solution may look ugly or un-Python-ish
Question
Are there any refactorings, suggestions or comments on this approach?
### ********************
## setup an array using append
ary_names = []
ary_names.append('alpha')
ary_names.append('bravo')
ary_names.append('charlie')
ary_names.append('delta')
ary_names.append('echo')
### ********************
## setup an array using triple-quoted string
ary_names = [
item.strip() for item in
"""
alpha
bravo
charlie
delta
echo
"""
.splitlines()
if(not item.strip()=='')
]
"""
Here is a ruby example for comparison
ary_names = %w[
alpha
bravo
charlie
delta
echo
]
"""