python这是什么数据类型?

python what data type is this?

我是python的新手,目前正在使用zeroconf库。

当我尝试在网络上注册服务时,我在函数定义中看到了这一点:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def register_service(self, info, ttl=_DNS_TTL):
   """Registers service information to the network with a default TTL
    of 60 seconds.  Zeroconf will then respond to requests for
    information for that service.  The name of the service may be
    changed if needed to make it unique on the network."""

    self.check_service(info)
    self.services[info.name.lower()] = info
    if info.type in self.servicetypes:
        self.servicetypes[info.type] += 1
    else:
        self.servicetypes[info.type] = 1
    now = current_time_millis()
    next_time = now
    i = 0
    while i < 3:
        if now < next_time:
            self.wait(next_time - now)
            now = current_time_millis()
            continue
        out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA)
        out.add_answer_at_time(DNSPointer(info.type, _TYPE_PTR,
                                          _CLASS_IN, ttl, info.name), 0)
        out.add_answer_at_time(DNSService(info.name, _TYPE_SRV,
                                          _CLASS_IN, ttl, info.priority, info.weight, info.port,
                                          info.server), 0)
        out.add_answer_at_time(DNSText(info.name, _TYPE_TXT, _CLASS_IN,
                                       ttl, info.text), 0)
        if info.address:
            out.add_answer_at_time(DNSAddress(info.server, _TYPE_A,
                                              _CLASS_IN, ttl, info.address), 0)
        self.send(out)
        i += 1
        next_time += _REGISTER_TIME

有人知道info是什么类型吗?

编辑
感谢您提供答案,它是一个ServiceInfo类。 除了docstring在搜索时提供此答案的事实。 我仍然不清楚:

  • 遇到这种情况时,流程专家python程序员会遵循-在没有文档字符串的情况下,要采取什么步骤查找info的数据类型?
  • 当我们不将类类型指定为register_service的输入参数的一部分时,python解释器如何知道info是ServiceInfo类的? 如何知道info.type是有效属性,而又说info.my_property不是?


它是ServiceInfo类的实例。

可以通过阅读代码和文档字符串来推断。 register_service调用check_service函数,我引用此函数"检查网络中的唯一服务名称,如果传入的ServiceInfo不是唯一的,则对其进行修改"。


看起来它应该是ServiceInfo。在存储库的示例中找到:

https://github.com/jstasiak/python-zeroconf/blob/master/examples/registration.py

编辑

  • 除了"我必须采取的任何方式"之外,我不太确定该说些什么。在实践中,我真的不记得还没有清楚清楚接口的约定,因为那只是使用Python的一部分。因此,更需要文档。
  • 简短的回答是,"没有"。 Python使用"鸭子类型"的概念,其中支持合同必要操作的任何对象均有效。您可以为它提供具有代码使用的所有属性的任何值,并且不知道它们之间的区别。因此,在第1部分中,最坏的情况是,您只需要追溯对象的每次使用,直到它传递为止,并提供一个满足所有要求的对象,如果您错过了一个部件,则会遇到运行时错误使用它的任何代码路径。
  • 我更喜欢静态类型。在很大程度上,我认为使用动态类型时文档和单元测试只是"更严格的要求",因为编译器无法为您完成任何工作。