Determine if a function is available in a Python module

后端 未结 3 1242
一生所求
一生所求 2021-02-01 15:59

I am working on some Python socket code that\'s using the socket.fromfd() function.

However, this method is not available on all platforms, so I am writing some fallback

3条回答
  •  南方客
    南方客 (楼主)
    2021-02-01 16:29

    Or simply use a try..except block:

    try:
      sock = socket.fromfd(...)
    except AttributeError:
      sock = socket.socket(...)
    

提交回复
热议问题