"a better input"
Alex Martelli
aleax at aleax.it
Thu May 9 08:30:53 EDT 2002
Michael Hudson wrote:
> Alex Martelli <aleax at aleax.it> writes:
>
>> > *I realize there are no true complex literals, we'd have to do a little
>> > extra work here
>>
>> What extra work?
>>
>> >>> complex('23+45j')
>> (23+45j)
>
> Well, how does cooked_input or whatever tell that 23+45j is a complex
> literal? The parser won't, at present...
What about:
def isThisAComplexLiteral(this):
try: complex(this)
except: return False
else: return True
if you *must* think in boolean terms?
Of course, the real approach is less silly:
def cooked_input(astr=None):
if astr=None: astr=raw_input
for atype in int, long, float, complex:
try: return atype(astr)
except: pass
else:
return astr
or variations thereon (stripping astr in the
final statement if you think that' better, &c).
Alex
More information about the Python-list
mailing list