译(三十一)-Python获取变量类型

文章首发及后续更新:https://mwhls.top/3188.html
新的更新内容请到mwhls.top查看。
无图/无目录/格式错误/更多相关请到上方的文章首发页面查看。

stackoverflow热门问题目录

如有翻译问题欢迎评论指出,谢谢。
尖括号打不出来,所以删掉了,类似type 'int’的地方原本是有<>的

目录
1. 如何在Python中确定变量类型?
2. How to determine a Python variable\’s type?
如何在Python中确定变量类型?
  • user46646 asked:

    • 如何获取变量的类型,例如无符号32位,有符号16位?
  • Answers:

    • gregjor – vote: 1759

      • 使用 type()内置函数:

      • >>> i = 123
        >>> type(i)
        type ‘int’
        >>> type(i) is int
        True
        >>> i = 123.456
        >>> type(i)
        type ‘float’
        >>> type(i) is float
        True

  • 为了确定变量类型与给定类型是否相同,使用 isinstance

  • >>> i = 123
    >>> isinstance(i, int)
    True
    >>> isinstance(i, (float, str, set, dict))
    False
  • 注意Python并没有如你所提问的,C/C++的类型。

  • Vaibhav – vote: 497

    • 你需要的应该是 type() 内置函数
    • 下面是例子,不过Python和java一样,没有无符号类型。
    • 正数:
    • >>> v = 10
      >>> type(v)
      type 'int'
    • 更大的正数:
    • >>> v = 100000000000000
      >>> type(v)
      type 'long'
    • 负数
    • >>> v = -10
      >>> type(v)
      type 'int'
    • 字符串序列:
    • >>> v = 'hi'
      >>> type(v)
      type 'str'
    • 浮点型正数:
    • >>> v = 3.14159
      >>> type(v)
      type 'float'
  • Aaron Hall – vote: 198

    • 很简单,像这样即可:
    • print(type(variable_name))

  • How to determine a Python variable\’s type?
    • user46646 asked:

      • How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?
        如何获取变量的类型,例如无符号32位,有符号16位?
      • How do I view it?
    • Answers:

      • gregjor – vote: 1759

      • Use the type() builtin function:
        使用 type()内置函数:

      • >>> i = 123
        >>> type(i)
        type 'int'
        >>> type(i) is int
        True
        >>> i = 123.456
        >>> type(i)
        type 'float'
        >>> type(i) is float
        True
      • To check if a variable is of a given type, use isinstance:
        为了确定变量类型与给定类型是否相同,使用 isinstance

      • >>> i = 123
        >>> isinstance(i, int)
        True
        >>> isinstance(i, (float, str, set, dict))
        False
      • Note that Python doesn\’t have the same types as C/C++, which appears to be your question.
        注意Python并没有如你所提问的,C/C++的类型。

      • Vaibhav – vote: 497

      • You may be looking for the type() built-in function.
        你需要的应该是 type() 内置函数

      • See the examples below, but there\’s no unsigned type in Python just like Java.
        下面是例子,不过Python和java一样,没有无符号类型。

      • Positive integer:
        正数:

      • >>> v = 10
        >>> type(v)
        type 'int'
      • Large positive integer:
        更大的正数:

      • >>> v = 100000000000000
        >>> type(v)
        type 'long'
      • Negative integer:
        负数

      • >>> v = -10
        >>> type(v)
        type 'int'
      • Literal sequence of characters:
        字符串序列:

      • >>> v = 'hi'
        >>> type(v)
        type 'str'
      • Floating point integer:
        浮点型正数:

      • >>> v = 3.14159
        >>> type(v)
        type 'float'
      • Aaron Hall – vote: 198

      • It is so simple. You do it like this.
        很简单,像这样即可:

      • print(type(variable_name))

你可能感兴趣的:(python,python,StackOverflow)