I don't know how to determine type of a given variable while I read Python code. I would like to know the types of variables without the deep knowledge of methods that initialize values for them. Say, I have piece of code:
我在阅读Python代码时不知道如何确定给定变量的类型。我想知道变量的类型,而没有深入了解为它们初始化值的方法。说,我有一段代码:
import numpy as np
np.random.seed(0)
n = 10000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
xmin = x.min()
xmax = x.max()
ymin = y.min()
ymax = y.max()
How do I know what type x
is? In Java it's simple. Even if I don't know the method, I know the variable type.
我怎么知道x是什么类型的?在Java中它很简单。即使我不知道方法,我也知道变量类型。
5 个解决方案
#1
3
You can use the builtin type
function to check the type of a variable.
您可以使用内置类型函数来检查变量的类型。
import numpy as np
np.random.seed(0)
n = 10000
x = np.random.standard_normal(n)
print(type(x))
# numpy.ndarray
If, in the specific case of numpy
, you want to check the type of your elements, then you can do
如果,在numpy的特定情况下,您想要检查元素的类型,那么您可以这样做
print(x.dtype)
# dtype('float64')
#2
2
Python is a dynamically typed language. Technically, when reading code, you won't be able to know the type of a variable without following the code, or if the code is excessively simple.
Python是一种动态类型语言。从技术上讲,在阅读代码时,如果不遵循代码,或者代码过于简单,您将无法知道变量的类型。
A few quotes for you:
给你一些报价:
Python is strongly typed as the interpreter keeps track of all variables types. It's also very dynamic as it rarely uses what it knows to limit variable usage.
Python是强类型的,因为解释器会跟踪所有变量类型。它也非常动态,因为它很少使用它所知道的来限制变量的使用。
In Python, it's the program's responsibility to use built-in functions like isinstance() and issubclass() to test variable types and correct usage.
在Python中,程序有责任使用内置函数(如isinstance()和issubclass()来测试变量类型和正确使用。
You can use isinstance(x, type)
or type(x)
to learn about the variables type information at runtime.
您可以使用isinstance(x,type)或type(x)来了解运行时的变量类型信息。
#3
1
type(x)
is straitforward answer. Normally you don't test type it through type
but use isinstance(x, type)
to test it.
type(x)是一个明确的回答。通常,您不测试通过类型键入它,但使用isinstance(x,type)来测试它。
#4
1
Use dtype
:
使用dtype:
n = 10000
x = np.random.standard_normal(n)
x.dtype
gives:
得到:
dtype('float64')
If you want more detailed information on the array attributes, you could use info
:
如果您想了解有关数组属性的更多详细信息,可以使用info:
np.info(x)
gives:
得到:
class: ndarray
shape: (10000,)
strides: (8,)
itemsize: 8
aligned: True
contiguous: True
fortran: True
data pointer: 0xba10c48
byteorder: little
byteswap: False
type: float64
#5
0
in the REPL (interactive console), you can also do
在REPL(交互式控制台)中,您也可以这样做
>>> help(x)
and it will display information about x
's class, including it's methods.
它将显示有关x的类的信息,包括它的方法。
#1
3
You can use the builtin type
function to check the type of a variable.
您可以使用内置类型函数来检查变量的类型。
import numpy as np
np.random.seed(0)
n = 10000
x = np.random.standard_normal(n)
print(type(x))
# numpy.ndarray
If, in the specific case of numpy
, you want to check the type of your elements, then you can do
如果,在numpy的特定情况下,您想要检查元素的类型,那么您可以这样做
print(x.dtype)
# dtype('float64')
#2
2
Python is a dynamically typed language. Technically, when reading code, you won't be able to know the type of a variable without following the code, or if the code is excessively simple.
Python是一种动态类型语言。从技术上讲,在阅读代码时,如果不遵循代码,或者代码过于简单,您将无法知道变量的类型。
A few quotes for you:
给你一些报价:
Python is strongly typed as the interpreter keeps track of all variables types. It's also very dynamic as it rarely uses what it knows to limit variable usage.
Python是强类型的,因为解释器会跟踪所有变量类型。它也非常动态,因为它很少使用它所知道的来限制变量的使用。
In Python, it's the program's responsibility to use built-in functions like isinstance() and issubclass() to test variable types and correct usage.
在Python中,程序有责任使用内置函数(如isinstance()和issubclass()来测试变量类型和正确使用。
You can use isinstance(x, type)
or type(x)
to learn about the variables type information at runtime.
您可以使用isinstance(x,type)或type(x)来了解运行时的变量类型信息。
#3
1
type(x)
is straitforward answer. Normally you don't test type it through type
but use isinstance(x, type)
to test it.
type(x)是一个明确的回答。通常,您不测试通过类型键入它,但使用isinstance(x,type)来测试它。
#4
1
Use dtype
:
使用dtype:
n = 10000
x = np.random.standard_normal(n)
x.dtype
gives:
得到:
dtype('float64')
If you want more detailed information on the array attributes, you could use info
:
如果您想了解有关数组属性的更多详细信息,可以使用info:
np.info(x)
gives:
得到:
class: ndarray
shape: (10000,)
strides: (8,)
itemsize: 8
aligned: True
contiguous: True
fortran: True
data pointer: 0xba10c48
byteorder: little
byteswap: False
type: float64
#5
0
in the REPL (interactive console), you can also do
在REPL(交互式控制台)中,您也可以这样做
>>> help(x)
and it will display information about x
's class, including it's methods.
它将显示有关x的类的信息,包括它的方法。