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 。值得注意的是,这里的“之前”是指“执行顺序”,而不是“源代码顺序”。关于这个问题还有更多细节(例如import语句,全局变量,命名空间挂钩),但我们现在保持简单。
要定义一个“没有值”的变量,只需将其赋值为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
另外,良好的Python实践是从”object”派生所有类,这样你就可以使用新式类并使用 小写加下划线 或 首字母小写并带有大写字母 的命名实例变量的约定。类名几乎总是以首字母大写的格式书写。
方法4:使用@dataclass装饰器
这个函数是一个装饰器,用于向类添加生成的特殊方法。
dataclass 模块是在Python 3.7中引入的,它是一个专门用于创建结构化类以供数据存储的实用工具。这些类具有处理数据及其表示的特定属性和函数。
示例
# importing dataclasses
from dataclasses import dataclass
@dataclass
class Student:
StudentName: str
rollNumber: str
这里我们创建了两个类型为字符串的类属性。默认情况下,学生姓名和学号的值将为 None。
结论
在本文中,我们学习了如何使用几种方法声明具有None值的类属性。我们还学习了面向对象编程中的init()函数和self关键字。
极客笔记