Good Style in Python Objects

后端 未结 5 1061
春和景丽
春和景丽 2021-01-19 23:42

Most of my programming prior to Python was in C++ or Matlab. I don\'t have a degree in CS (almost completed a PhD in physics), but have done some courses and a good amount o

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-20 00:15

    For checking if an argument is of the expected type, use the built-in isinstance() function:

    if not isinstance(leader1, UnionFind):
        raise ValueError('leader1 must be a UnionFind instance')
    

    Additionally, it is a good habit to add docstrings to functions, classes and member functions. Such a docstring for a function or method should describe what it does, what arguments are to be passed to it and if applicable what is returned and which exceptions can be raised.

提交回复
热议问题