Python无类型

In this tutorial we are going to discuss about Python NoneType. Before we start discussion on NoneType let us first see what is an object.

在本教程中,我们将讨论Python NoneType。 在开始讨论NoneType之前,让我们首先看看什么是对象。

In any programming language preliminary data types are int, float, char etc. and we can create a class by mixing different data types together and the instances of these classes are called as object.

在任何编程语言中,初步的数据类型都是int,float,char等,我们可以通过将不同的数据类型混合在一起来创建一个类,这些类的实例称为对象。

Take this for example:

以这个为例:

class student(object):
    roll = 5
 
stu = student()
print(stu.roll)

Here stu is and object of class student.

这里的斯图是班级学生的对象。

When stu = student() is executed an object is created but what if we want to create the object stu but does not want to assign any value.

当执行stu = student()时,将创建一个对象,但是如果我们要创建对象stu但不希望分配任何值,该怎么办。

Here we can use None keyword.

在这里,我们可以使用None关键字。

None is actually an object of the class NoneType. When we write

None实际上是NoneType类的对象。 当我们写

stu = None we are actually pointing to an object of the class NoneType which always have a special name None.

stu = None我们实际上指向的是NoneType类的对象,该对象始终具有特殊名称None。

We can check the type of the object using type checking in python.

我们可以使用python中的类型检查检查对象的类型

class student(object):
    roll = 5
stu = student()
print(type(stu))
stu = None
print(type(stu))

The code above checks the class of stu before and after assigning None. The output is as below:

上面的代码在分配None之前和之后检查stu的类。 输出如下:

<class ‘__main__.student’> <class ‘NoneType’>

<class'__main __。student'> <class'NoneType'>

At start stu belongs to student class but later it’s class is changed to NoneType.

最初,stu属于学生班级,但后来它的班级更改为NoneType。

One important thing to keep in mind is that there can be one and only one None object in your whole python code. Each object which is pointing to None will point to the same object.

要记住的一件事是,整个Python代码中只能有一个None对象。 指向无的每个对象都将指向同一对象。

stu = None
print(id(stu))
teacher = None
print(id(teacher))

id() is used to get the unique identifier assigned to objects in Python. As expected the above code will print the same value both of the times.

id()用于获取分配给Python对象的唯一标识符。 如预期的那样,以上代码将两次打印相同的值。

Output:

输出:

9918176

9918176

9918176

9918176

The value can be different than shown but it will print the same value for both of the statements.

该值可以与显示的值不同,但是它将为两个语句打印相同的值。

What is the use of None?

None的用途是什么?

None serves the purpose of NULL in other languages in Python.

在Python中的其他语言中,None没有达到NULL的目的。

  • None is used when an object is not initiated with a value.

    当不使用值初始化对象时,不使用None。
  • None is the return type of every function which does not return anything.

    每个函数都不返回任何值的返回类型为None。
  • None is returned when searched keyword is not found in dictionary.

    在词典中找不到搜索到的关键字时,不返回任何内容。

How can we check if the given object is of type None?

我们如何检查给定对象是否为None类型?

The best way to check if the object is of type None is to use the is keyword.

检查对象是否为None类型的最佳方法是使用is关键字。

class student(object):
    roll = 5
 
stu = student()
print(stu is None)  # Prints False
stu = None
print(stu is None)  # Prints True.

== should not be used for checking the type of the object with None as  PEP 8 explicitly states that “comparisons to singletons like None should always be done with is or is not, never the equality operators.”

==不应使用None来检查对象的类型,因为PEP 8明确指出 “对单例的比较(如None,应始终使用is or not进行 ,永远不要使用等于运算符。”)

Comment down below if you have any queries related to python nonetype.

如果您有任何与python nonetype相关的查询,请在下面注释掉。

翻译自: https://www.thecrazyprogrammer.com/2019/09/python-nonetype.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值