Tertiary Operation
Fredrik Lundh
fredrik at pythonware.com
Tue Oct 17 12:28:20 EDT 2006
Roy Smith wrote:
> Why not just:
>
> if x is None:
> result = str(x)
> else:
> result = ""
>
> It's a couple more lines of code, but it's obvious what it means.
and if you're doing this a few times, putting it in a function is even
better.
def tostring(obj):
if obj is None:
return ""
return str(obj)
print tostring(key)
print tostring(foo), tostring(bar)
file.write(tostring(record[1]))
this also makes it easier to tweak things once you realize that not
everything should be passed through str():
def tostring(obj):
if obj is None:
return ""
if isinstance(obj, basestring):
return obj
return str(obj)
</F>
More information about the Python-list
mailing list