You should be able to do continuethis loop, like any other:
try:
yield int(item)
continue
except (ValueError, TypeError) as WrongTypeError:
pass
As a note, I always thought that continuewas a strange name for this management structure ...
And here is your corrected code in action:
def type_convert(data):
for item in data:
try:
yield int(item)
continue
except (ValueError, TypeError) as WrongTypeError:
pass
try:
yield float(item)
continue
except (ValueError, TypeError) as WrongTypeError:
pass
yield item
for a in type_convert(['a','1','1.0']):
print (a)
source
share