Should I force Python type checking?

前端 未结 7 1954
一个人的身影
一个人的身影 2020-12-09 04:56

Perhaps as a remnant of my days with a strongly-typed language (Java), I often find myself writing functions and then forcing type checks. For example:

def o         


        
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-09 05:54

    This is a non-idiomatic way of doing things. Typically in Python you would use try/except tests.

    def orSearch(d, query):
        try:
            d.get(something)
        except TypeError:
            print("oops")
        try:
            foo = query[:2]
        except TypeError:
            print("durn")
    

提交回复
热议问题