如何在 Python 中宣告一個沒有值的屬性?
在本文中,我們將向您展示如何在 Python 中宣告一個沒有值的屬性。
在 Python 以及其他幾種語言中,存在一個表示“無值”的值。在 Python 中,這個無值的值是None,讓我們看看它是如何使用的。
你根本做不到。在 Python 中,變數只是名稱。名稱總是指代一個物件(“已繫結”)。按照慣例,將那些尚未具有有意義的值但應該存在的名稱設定為 None。
方法一:直接用 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
方法二
演算法(步驟)
以下是執行所需任務的演算法/步驟:
建立一個名為Student的類。
建立一個__init__建構函式,將 StudentName 和 RollNumber 作為引數傳遞給它。
在建構函式內部,使用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
方法三
為什麼我們需要初始化屬性?
讓我們透過在不為例項屬性賦值的情況下列印它們的值來回答這個問題
示例
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”派生所有類,以便您可以使用新式類並使用lowercase_with_underscore或initialLowerWithCaps約定命名例項變數。類名幾乎總是以 InitialCaps 格式編寫。
方法四:使用 @dataclass 裝飾器
此函式是一個裝飾器,用於向類新增生成的特殊方法。
dataclass模組在 Python 3.7 中引入,作為建立專門用於資料儲存的結構化類的實用工具。這些類具有處理資料及其表示的特定屬性和函式。
示例
# importing dataclasses from dataclasses import dataclass @dataclass class Student: StudentName: str rollNumber: str
在這裡,我們建立了兩個型別為字串的類屬性。預設情況下,學生姓名和學號的值將為None。
結論
在本文中,我們學習瞭如何使用幾種方法用 None 值宣告類屬性。我們還學習了面向物件程式設計中的 __init__() 函式和 self 關鍵字。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP