6
mr-user
5y

Thank python. Thank you , you just waste a hour of my life. The function is silently fail and spend a hour trying of to debug it.After a hour you know what a problem is? The parameter type.

The function expect a string but I am putting non-string type in the parameter and it just silently fail with no exception thrown. Great!

Comments
  • 0
    @pythonInRelay I use pylinter but got no warning. Does it sliently cast to string?
  • 1
    Don't typehints catch errors in your IDE like that?
  • 0
    Both os.listdir and str.startswith throw a TypeError for me.
    Python rarely silently casts types.

    By the way, path.os.listdir is just a fancy way to call os.listdir. You probably want .iterdir() of a Path object.
  • 0
    @sbiewald For me it silently cast when the type is <class 'numpy.int64'> , I just have to cast it to string with str() method before passing it to function.
  • 0
    I use type hint as much as possible but it's hard to determine the return type of library.

    I know it's a python (dynamic typing langue) feature but the function should only return 1 specific type.

    The function may return the bool if fail and when success.

    def divide(a:int ,b:int):

    if b==0:

    return False

    return a/b
  • 0
    > cast it to string with str() method

    @mr-user It's converting, not casting. I guess the lines are blurred in Python (that's what you get from a dynamic-only language) but please try and not mix up these two concepts.
  • 0
    @SomeNone

    What is the line between casting and converting in python? Since inheritance are rarely used in python.

    Casting : Changing based on the inheritance. Male inheritance from Person so we can cast a male object to person object.Think polymorphism.

    Converting: Convert the one type to another type.Doesn't need to have relationship between each other.For example every object can be converted to and from json/string.
  • 2
    **pulls out cigar**
    What if I told you...
    **lights lighter**
    That there are...
    **lights up the cigar and takes a puff**
    Strong typed languages out there?
  • 0
    @DubbaThony I would tell "I invent Strongly typed languages"

    Joke aside I mainly code in the strongly typed language and the transaction to dynamically typed language has been uncomfortable to me.
  • 0
    @mr-user

    This sounds like story of younger Dubbs who switched from c++ to php 5.

    Than php 7.0 went out and life got better
  • 0
    @DubbaThony You mean statically types languages?

    Static: Variables have a declared type and you cannot change the type of the variable. (E.g. C, Rust, ...)
    Strong: There are types and they are different (Rust, Python; C, ...)

    Dynamic: Variables have no declared type, a variable can be reassigned with a different type. (JavaScript, Python)
    Weak: Types? What are types? (JavaScript)
  • 1
    @sbiewald

    I started my programming in statically typed language.

    Than I switched to PHP 5 where its dynamic, than php 7.0 with dynamic strong typing came out and it was the perfection of having best of both worlds for me :D

    Anyway, i ment all in all in my orig comment statically typed languages ;)
Add Comment