简体   繁体   English

Python 带印刷字的铸件

[英]Python Type Casting with Print

Why does this return a class/type of 'NoneType'?为什么这会返回“NoneType”的类/类型?

The input is a string.输入是一个字符串。 I have cast type to an int...我已将类型转换为 int ...

first_digit = print(int(two_digit_number[0]))
print(type(first_digit))

I know that the following will return class/type of int:我知道以下将返回 int 类/类型:

first_digit = int(two_digit_number[0])

But, I don't understand why adding print creates a NoneType class. FYI, I'm on day 2 of learning Python.但是,我不明白为什么添加 print 会创建 NoneType class。仅供参考,我在学习 Python 的第 2 天。

Functions that don't return anything return a None (of NoneType ) by default.默认情况下,不返回任何内容的函数返回None (属于NoneType )。 Print doesn't return what it printed or anything else, just None . Print 不返回它打印的内容或其他任何东西,只是None

print function doesn't have a return value. print function 没有返回值。 Assigning it to a variable makes its value None of type NoneType .将其分配给变量使其值None类型为NoneType

Print is a function to print something on screen it did not return anything to user so it return None Print是一个 function 在屏幕上打印一些东西它没有返回任何给用户所以它返回None

In its most simple view you are doing this:在最简单的视图中,您正在这样做:

first_digit = print(1)

What's the value of first_digit ? first_digit的值是多少? It is NoneType because you did not assign any value to the first_digit .它是NoneType因为您没有为first_digit分配任何值。

Try this instead:试试这个:

[ins] In [14]: first_digit = 1

[ins] In [15]: print(type(first_digit))
<class 'int'>

Regards问候

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

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