Skip to content

simplifying the logic in the isNumber function in pygmykernel.py #2

@franksergeant

Description

@franksergeant

I received these comments via email:

I learned isdigit will match some non-decimal number systems,
so just for fun, I tried to paste in a Kharosthi number from
Wikipedia. It didn't work, Windows and Python spoiled the fun
with character encoding complexity before it could even get to
int().

Huh... int() will refuse to convert a string containing a
decimal point. That's technically good, it would remove any
possibility of mismatch between what could be an int and what
our if statements determine. It could be used with try...except
just like floats, but I wouldn't want to indent the rest of the
function inside the except block. What do you think of the
following code which uses return to elide further flow
control? "pass" here signifies "ValueError doesn't matter yet,
there are other things to try." Sorry if I'm fussing too much,
I'm still developing my programming style.

try:
    return (True, int(s))
except ValueError:
    pass
try:
    return (True, float(s))
except ValueError:
    pass
# (then try chars & hex)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions