如何在 Python 中声明没有值的属性?
在本文中,我们将向您展示如何在 Python 中声明没有值的属性。
在 Python 以及其他几种语言中,有一个值表示"无值"。在 Python 中,没有值的值是 None,让我们看看它是如何使用的。
你根本做不到。变量只是 Python 中的名称。名称始终引用对象("已绑定")。将尚未具有有意义的值但应该存在的名称设置为 None 是常规做法。
方法 1:直接用 None 初始化它们
我们可以直接用 None 值分配属性。通过创建一个类并用 None 初始化类中的两个变量。
# creating a class with the name Student class Student: # initiaizing the values of instance variables with None StudentName = None RollNumber = None
不过,这些变量更像是实例变量,而不是类变量,因此我们不妨这样写
与其像上面那样写,我们可以使用 self 关键字在构造函数 init__() 方法中实例化实例变量(在 Python 中,__init__ 是保留方法之一。在面向对象编程中,它被称为构造函数。当从类创建对象并且需要访问以初始化类的属性时,将调用 __init__ 函数)。
此处的 self 用于指向实例变量(self 表示类的实例。它将属性与提供的参数绑定。我们使用 self 是因为 Python 不使用"@"语法来引用实例属性))
示例
# creating a class with the name Student class Student(object): # creating a init constructor def __init__(self): # initiaizing the values of instance variables with None self.StudentName = None self.RollNumber = None
方法 2
算法(步骤)
以下是执行所需任务需要遵循的算法/步骤 -
创建一个名为 Student 的类。
通过将 StudentName 和 RollNumber 作为参数传递给它来创建 __init__ 构造函数。
在构造函数内部,使用 self 属性初始化实例变量的值(self 表示类的实例。它将属性与提供的参数绑定在一起)。
通过将 StudentName 和 RollNumber 参数值作为 None 传递并调用它来创建 Student 类的对象。
使用上述学生类打印 StudentName 属性的值对象。
使用 (.) 符号使用上述学生类对象打印 RollNumber 属性的值。此处 (.) 符号用于访问该属性。
示例
# creating a class with the name Student class Student(object): # creating an init constructor by passing the StudentName, and RollNumber as arguments to it def __init__(self, StudentName, RollNumber): # initiaizing the values of instance variables self.StudentName = StudentName self.RollNumber = RollNumber # creating an object of the Student class by passing # StudentName, RollNumber values as None and calling it studentObj= Student(None, None) # printing the value of the StudentName attribute print('The value of the StudentName attribute is:',studentObj.StudentName) # printing the value of the roll number attribute print('The value of the RollNumber attribute is:',studentObj.RollNumber)
输出
The value of the StudentName attribute is: None The value of the RollNumber attribute is: None
方法 3
为什么我们需要初始化属性?
让我们通过打印实例属性的值而不为其分配任何值来回答这个问题
示例
class Student: StudentName RollNumber print(StudentName, RollNumber)
输出
Traceback (most recent call last): File "main.py", line 1, in <module> class Student: File "main.py", line 2, in Student StudentName NameError: name 'StudentName' is not defined
StudentName 和 RollNumber 以及 print(StudentName, RollNumber) 被解析为表达式。由于尚未赋值,因此您会得到 NameError 。
Python 中的变量必须先使用赋值语句定义,然后才能在表达式中使用;否则,将返回 NameError。值得注意的是,这里的"之前"是指"执行顺序",而不是"源代码顺序"。还有更多内容(导入语句、全局变量、命名空间 hack),但现在我们先简单介绍一下。
要定义"没有值"的变量,只需为其赋值 None,这是惯用方式
此外,您的代码似乎在寻找实例成员,而不是类成员。一些静态分析工具(例如 pylint)识别出实现该任务的惯用方法,如下所示:
算法(步骤)
以下是执行所需任务需要遵循的算法/步骤 -
在构造函数中,使用 self 关键字将实例变量的值初始化为 None,并使用 self 关键字和 (.) 运算符打印学生姓名和学号的值
创建 Student 类的对象。然后它将默认调用 __init__ 方法(此处初始化并打印构造函数中的值)。
示例
# creating a class with the name Student class Student(object): # constructor of the student class def __init__(self): # initiaizing the values of attributes with None self.studentName = None self.rollNumber = None print('The Value of Student Name:',self.studentName) print('The Value of Roll Number:',self.rollNumber) #Creating the Object for the student class studentObject=Student()
输出
The Value of Student Name: None The Value of Roll Number: None
此外,从"object"派生所有类是一种很好的 Python 做法,这样您就可以利用新式类并使用 lowercase_with_underscore 或 initialLowerWithCaps 约定命名实例变量。类名几乎总是以 InitialCaps 格式书写。
方法 4:使用 @dataclass 装饰器
此函数是一个用于将生成的特殊方法添加到类的装饰器
dataclass 模块在 Python 3.7 中引入,作为创建专为数据存储设计的结构化类的实用工具。这些类具有处理数据及其表示的特定属性和函数。
示例
# importing dataclasses from dataclasses import dataclass @dataclass class Student: StudentName: str rollNumber: str
这里我们创建了两个字符串类型的类属性。默认情况下,学生姓名和学号的值将为None。
结论
在本文中,我们学习了如何使用几种方法声明具有 None 值的类属性。我们还了解了面向对象编程中的 __init__() 函数和 self 关键字。
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

