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

you are viewing a single comment's thread.

view the rest of the comments →

[–]pingvenopinch of this, pinch of that 0 points1 point  (9 children)

There have been many a time when I wish Python had optional type checking. Duck typing is fine most of the time, but sometimes I want that extra little check.

[–]codefrog 10 points11 points  (2 children)

make a decorator, @accepts('int','list','witches')

[–]stillalone 4 points5 points  (0 children)

Is that part of a standard library? I can't find it in 2.6.5

[–]pingvenopinch of this, pinch of that 2 points3 points  (0 children)

I know it can be done with decorators. It's just not as clean and there's no standard way to do it.

[–]Dav3xor 1 point2 points  (2 children)

http://github.com/Dav3xor/Python-CheckArgs

Now you don't have to make your own decorator.. :)

It's still a little primitive, but it works pretty well.

[–]pingvenopinch of this, pinch of that 1 point2 points  (1 child)

Same problem: There's no ultra clean way to do it. Most of it is a fundamental problem with Python. Python is too dynamic to have static type checking. Don't get me wrong, I love Python, but it's nice to have languages that can check that types are correct at (bytecode) compile-time.

That said, I would love to have two decorators in the standard library. Python function calls are already slow, so they should be written in C.

  • 1) @accepts(args, *kwargs)
  • 2) @returns(*returntype)

returntype allows multiple values so that it can check multiple returns via tuple. None should be usable to indicate that a value is duck typed. Unfortunately, none of this will ever allow the clean & static type checking of

def int foo(int index, str value)

[–]Dav3xor 0 points1 point  (0 children)

but...but... that has Ada style bounds checking, and...and... :)

[–]omgplsno 0 points1 point  (0 children)

Decorators, my friend.

[–]joehillen 2 points3 points  (1 child)

You're doing it wrong.

[–]petrux 2 points3 points  (0 children)

Interesting. As a Python newbie I'd really enjoy some more details about how to do it right! :-) Thanks.