简体   繁体   中英

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

I wrote a program of two lines in python. At first I tested it in the python shell. Here it is:

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

The output was as I expected in the shell. And then I wrote these instructions in a file named main.py.

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

Then I executed this program using linux terminal as root user. 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. 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. It's what is called when you type an object in Python's interactive shell. When you're in a file you need to print the result using print function that triggers the __str__ method.

state=True
print(type(state))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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