Hacker News new | past | comments | ask | show | jobs | submit login

Yes, in theory this is a problem in lots of languages which are weakly typed. There are lots of ways of handling this, some generic and some language specific.

In python I would suggest wrapping your call in a try / expect block or using "isinstance()".

If you are using a publicly available library or popular piece of code that can return None or an Integer, I would argue that piece of code was written incorrectly. Newbie python users might do this, but I think experienced python devs would see the problem.

Finally, some documentation or justification for the reasoning behind this decision at the top of the function or on a web page somewhere would help as well.

As with most powerful , full featured languages it is pretty easy to shoot yourself in the foot if you are not careful, I don't think this is a python specific problem...




I would counter that isinstance() checking is - at least in the general case - an antipattern.

Better to check that the object you've been passed implements the interface you need rather than reasoning about its inheritance chain. Duck typing!


Some times one wants classes to be semantic, not only structural. That means that when the developer writes "class C(Foo)" he means something different than writting "class C(Bar)" even if both classes implement the same methods.


As much as I hate the Java language, "isinstance" does allow you to check for an interface, not just a class.


>"Yes, in theory this is a problem in lots of languages which are weakly typed."

Minor pedantic correction: Python is dynamically typed, and strongly typed. Those are separate concepts.

    Strongly-typed vs weakly-typed
    Statically-typed vs dynamically-typed


We really need a language that defaults to statically typed, but that can be dynamically typed where you ask nicely.

E.g. - as opposed to the Java practice of having 3 or more layers of fun to read (NOT!) XML on top of the language's native static typing to glue things together. I'm gonna be sick now...




Have you looked at Haskell?

It's not really dynamic typed, but has no requirements of making the types explicit, nor of making your functions work over a defined set of types.


Python is strongly typed.


True. What most people are complaining about is that it's dynamically typed, not weakly typed. That's where most of the confusion sets in.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: