Force a function parameter type in Python?

前端 未结 2 1465
有刺的猬
有刺的猬 2020-12-16 01:22

I have a function in a Python class that adds Interfaces to a list.

def RegisterAsListener(self, inListener):
    self.__TransitListeners.append(inListener)
         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 02:09

    Since i'm not the only one on this project, I don't want somebody calling the RegisterAsListener function with an incorrect data type

    Document the function thoroughly, then let incorrect parameters throw exceptions. The user of RegisterAListener should be able to use the documentation to know what kind of data the function expects, and - if given incorrect parameters - the resulting exception should make it clear what the user of RegisterAListener did wrong.

提交回复
热议问题