2015-08-22 87 views
0

我很新的python,目前正在玩zeroconf库。python这是什么数据类型?

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

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类的答案。除了文档字符串在人们搜索时提供这个答案的事实。我还是不明确的:

  • 过程中遇到这种情况时,专家Python程序员遵循 - 文档字符串的时候没有采取什么措施来找到info数据类型说呢?
  • python解释器如何知道info是ServiceInfo类的,当我们没有指定类类型作为register_service的输入参数的一部分?它如何知道info.type是一个有效的属性,并且说info.my_property不是?
+0

你在哪里看到这个定义?我似乎无法找到'register_service'在https://github.com/wmcbrine/pyzeroconf – ryachza

+0

我要添加'info .__ class__'到'register_service'函数,但我不能调用该函数作为' info'是一个输入参数。任何其他方式我可以调用该函数?如果我添加了'info = None',它会使数据类型失效吗? – snowbound

+0

哎呀,对不起@ZJM。错误的链接。这是正确的:https://github.com/jstasiak/python-zeroconf – snowbound

回答

2

它是ServiceInfo类的一个实例。

从阅读代码和docstrings可以推断出它。 register_service调用check_service函数,我引用“检查网络中唯一的服务名称,修改传入的ServiceInfo是否不唯一”。

+0

hi @Alik。你看到了哪些文档?我在'zeroconf.py'模块中搜索了'info'的所有实例,并且找不到它的正确定义。只看到它用。 (点)符号,例如'info.type','info.address' – snowbound

+0

@snowbound我已经更新了我的答案 – Alik

+2

@snowbound:欢迎来到动态语言的世界!你应该从这里拿回家,就是在自己的代码中编写出好的文档,记录不明显的数据类型。 :) –

1

它看起来应该是ServiceInfo。发现在仓库中的例子:

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

编辑

  1. 我真的不知道该说些什么,除了“什么办法,我要”。在实践中,我无法真正记得接口合约未被完全清楚的时候,因为这只是使用Python的一部分。由于这个原因,文档更多是要求
  2. 简短的回答是,“它不”。 Python使用“鸭子打字”的概念,其中任何支持合同必要操作的对象都是有效的。你可以给它任何值的代码使用所有的属性,它不知道区别。因此,根据第1部分,最糟糕的情况是,您只需要追踪对象的每次使用情况,并提供满足所有要求的对象,并且如果您错过了某个部分,则会收到运行时错误任何使用它的代码路径。

我的偏好也适用于静态打字。很大程度上,我认为使用动态类型进行文档和单元测试会变得“更难的要求”,因为编译器无法为您完成任何工作。

+0

嗨@ryachza,我已经更新了我的原始问题,以说出我不是清楚(对不起,我的背景是强类型/编译语言) – snowbound