简体   繁体   English

在python脚本文件中使用时,type()函数不起作用

[英]type() function doesn't work when used in a python script file

I wrote a program of two lines in python. 我用python写了两行程序。 At first I tested it in the python shell. 首先,我在python shell中对其进行了测试。 Here it is: 这里是:

>>>state=True
>>>type(state)
<class 'bool'>

The output was as I expected in the shell. 输出与我在shell中预期的一样。 And then I wrote these instructions in a file named main.py. 然后,我将这些指令写在一个名为main.py的文件中。

#---------------main.py----------------#
state=True
type(state)

Then I executed this program using linux terminal as root user. 然后,我使用Linux终端作为root用户执行了该程序。 The output was nothing 输出什么都没有

[manjaro ~]# python main.py
[manjaro ~]# 

I expected that the output would be as it was in the shell. 我期望输出将与在外壳中一样。 As a beginner In python I don't know why there was no output. 作为初学者,在python中,我不知道为什么没有输出。 Please help me to understand why there was no output. 请帮助我了解为什么没有输出。

What you see is the raw representation of the object which is returned by __repr__ method of the respective object. 您将看到对象的原始表示形式,该表示形式由相应对象的__repr__方法返回。 It's what is called when you type an object in Python's interactive shell. 当您在Python的交互式shell中键入对象时,即称为这种情况。 When you're in a file you need to print the result using print function that triggers the __str__ method. 当您在文件中时,需要使用触发__str__方法的print功能来打印结果。

state=True
print(type(state))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM