我会做...
word = ("could" if not condition() else "could not")
print "A five ounce bird {0} carry a one pound coconut".format(word)
:P
编辑:对于一般情况,这就是你想要的,我会按组合来。例如。(好吧,这对我来说太 Go4 和简单化了,但说明了这一点):
class Agglutinator(list):
def __str__(self):
return self._separator.join(str(x) for x in self)
class Paragraph(Agglutinator):
"""Returns dot separated sentences"""
_separator = '. '
class Sentence(Agglutinator):
"""Returns comma separated clauses"""
_separator = ', '
class Clause(Agglutinator):
"""Returns space separated words"""
_separator = ' '
c1 = Clause(["A", "number", "of", "words", "make", "a", "clause"])
c2 = Clause(["and", "a", "number", "of", "clauses", "make", "a", "sentence"])
c3 = Clause(["A", "collection", "of", "sentences", "makes", "a", "paragraph"])
s1 = Sentence([c1, c2])
s2 = Sentence([c3])
print Paragraph([s1, s2])
这给了你:
A number of words make a clause, and a number of clauses make a sentence. A collection of sentences makes a paragraph
详细说明一下,您可以将Sentence
第一个单词大写,等等。