【问题标题】:A tuple is not defined, but it contains information元组未定义,但包含信息
【发布时间】:2022-01-16 11:55:43
【问题描述】:

我正在寻找一个模块 ifcopenshell,我看到一个非常奇怪的功能:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import functools
import numbers
import itertools
from . import ifcopenshell_wrapper
try:
    import logging
except ImportError as e:
    logging = type('logger', (object,), {'exception': staticmethod(lambda s: print(s))})
class entity_instance(object):
    def __init__(self, e):
        if isinstance(e, tuple):
            e = ifcopenshell_wrapper.new_IfcBaseClass(*e)
        super(entity_instance, self).__setattr__('wrapped_data', e)

在这种情况下,tuple 没有在任何地方显式定义,但它包含一个值。我的假设是 tuple 来自进口。那正确吗?如何追踪tuple 的来源?

这里是函数isininstance的注释:

定义:isinstance(o: object, t: Union[type, Tuple[Union[type, Tuple[Any, ...]], ...]], /) -> bool 返回对象是类的实例还是其子类的实例。 一个元组,如 isinstance(x, (A, B, ...)),可以作为检查的目标。这相当于 isinstance(x, A) 或 isinstance(x, B) 或 ... 等。

【问题讨论】:

  • 我不知道你的问题是什么。你在问什么元组?
  • tuple 是内置的,如printint。它默认在所有模块中可用。
  • 未定义的元组如何包含信息?
  • @defladamouse 我正在告诉 isinstance 函数的元组
  • @Brian 但是 isinstance 函数会比较 e 和 tuple,e 是否包含在 tuple 中。默认元组为空。那么函数 isinstance 永远不会被使用?

标签: python module tuples


【解决方案1】:

tuplebuilt-in 类型,如intstr。自己试试吧:

>>> x = (3, 4)
>>> tuple
<class 'tuple'>
>>> isinstance(x, tuple)
True
>>> isinstance(x, str)
False

【讨论】:

  • 被函数isinstance的注释弄糊涂了,觉得里面一定有一些信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-15
  • 1970-01-01
  • 2014-05-18
  • 1970-01-01
  • 2021-05-01
  • 1970-01-01
相关资源
最近更新 更多