12 ms·
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.
by mox1 12y ago
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...
- leephillips 12y agoPython is strongly typed.
- g-clef 12y agoTrue. What most people are complaining about is that it's dynamically typed, not weakly typed. That's where most of the confusion sets in.
- chucksmash 12y agoI 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!
- Roboprog 12y agoAs much as I hate the Java language, "isinstance" does allow you to check for an interface, not just a class.
- marcosdumay 12y agoSome 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.
- zo1 12y ago>"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
- Roboprog 12y agoWe 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...
- zo1 12y agoSuch as C#'s dynamic objects, perhaps? https://msdn.microsoft.com/en-us/library/vstudio/ee461504%28v=vs.100%29.aspx https://msdn.microsoft.com/en-us/library/vstudio/ee461504%28...
- pseudonom- 12y agohttps://www.haskell.org/platform/doc/2014.2.0.0/ghc/users_guide/defer-type-errors.html https://www.haskell.org/platform/doc/2014.2.0.0/ghc/users_gu...
- marcosdumay 12y agoHave 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.