如何判断在已安装的modu中可用的方法

2024-05-23 13:44:20 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我在Python提示符下执行print(dir(pychromecast)),我将得到此消息底部的列表。哪些方法应该有效?因为有些会抛出一个属性错误,比如pychromecast.get_device_status(),而有些则像_get_chromecast_from_hostget_chromecasts抛出一个属性错误,比如:

AttributeError: module 'pychromecast' has no attribute 'get_chromehosts'

是不是我在做/理解一些根本错误的事情?名字前面的_是否也起作用?你知道吗

如前所述,这里是dir的输出:

['APP_BACKDROP', 'APP_DASHCAST', 'APP_MEDIA_RECEIVER', 'APP_PLEX',
 'APP_SPOTIFY', 'APP_YOUTUBE', 'CAST_TYPES', 'CAST_TYPE_CHROMECAST',
 'Chromecast', 'ChromecastConnectionError', 'ControllerNotRegistered',
 'DeviceStatus', 'IDLE_APP_ID', 'IGNORE_CEC', 'LaunchError',
 'MultipleChromecastsFoundError', 'NoChromecastFoundError', 'NotConnected',
 'PyChromecastError', 'PyChromecastStopped', 'STREAM_TYPE_BUFFERED', 
 'UnsupportedNamespace', '_LOGGER', '__all__', '__builtins__', '__cached__',
 '__doc__', '__file__', '__loader__', '__name__', '__package__', 
 '__path__', '__spec__', '__version__', '__version_info__', 
 '_get_chromecast_from_host', '_get_chromecast_from_service', 
 'cast_channel_pb2', 'config', 'controllers', 'dial', 
 'discover_chromecasts', 'discovery', 'error', 'fnmatch', 'get_app_config', 
 'get_chromecasts', 'get_device_status', 'get_possible_app_ids', 'json', 
 'logging', 'reboot', 'requests', 'socket_client', 'start_discovery',
 'stop_discovery', 'threading']

Tags: fromapphostget属性devicetypestatus
2条回答

他们都应该“工作”。其中有些是方法,有些是变量。你试过_get_chromecast_from_host_get_chromecast_from_host()吗?至于属性前面的uu或uuu,这意味着它受到保护,您可能不应该直接修改它,尽管只读取它就可以了。至少这是我的理解:)

But others like _get_chromecast_from_host or get_chromecasts throw an attribute error like ...

问题的一部分似乎是打字错误。您提到get_chromecasts,但您的例外行提到chrome主机

AttributeError: module 'pychromecast' has no attribute 'get_chromehosts'

有没有可能您在调用_get_chromecast_from_host时也有输入错误,或者提供了错误的参数?你能提供那个函数调用的具体例外情况吗?你知道吗

相关问题 更多 >