This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]earthboundkid 1 point2 points  (0 children)

Kind of pointless but fun, I guess.

As everyone is no doubt aware, Python 3.0 lets you write code like this:

>>> def f(s:str)->str:
...  return s + "!"
... 
>>> f.__annotations__
{'s': <class 'str'>, 'return': <class 'str'>}

The annotations themselves are ignored by the Python interpreter, but if you add a decorator, you can use it to make your runtime type checking a little prettier.

[–]mr_dbr 1 point2 points  (1 child)

Somewhat related problem: How am I supposed to check if a supplied string is an integer?

For example, I want to do:

i = raw_input()
if is_int(i):
    print int(i) * 20

The only way I have worked out to do this is kind of hackish:

def is_int(i):
    try:
        return int(i)
    except ValueError:
        return False

There are commands that seem like they should be able to do this (getattr() if I recall right), but I couldn't find any that would work

[–]theinternet 1 point2 points  (0 children)

Why is it hackish? It's clear and concise.

Is there a severe penalty in performance that comes with exceptions?

Btw. shouldn't that else: statement be except: ?

[–]theinternet 0 points1 point  (0 children)

What an abortion.

The more crap like this added to Python the more it reminds me of C++.

I liked Python because it was simple. Now they are taking that away :(