The task to define a class in Python.
在Python中定義類的任務。
Here, we are defining a class named Number with an attribute num, initializing it with a value 123, then creating two objects N1 and N2 and finally, printing the object's memory locations and attributes value using the object names.
在這里,我們定義一個名為Number的類,其屬性為num ,將其初始化為值123,然后創建兩個對象N1和N2 ,最后,使用對象名打印該對象的內存位置和屬性值。
定義類的Python代碼 (Python code to define a class)
# Python code to define a class
# class definition
class Number():
#Attribute
num = 123
# main code
if __name__ == "__main__":
# creating first object
N1 = Number()
#Printing object's memory (in hexadecimal)
print(N1)
#Accessing and printing Class's Attribute
print(N1.num)
# creating first object
N2 = Number()
#Printing object's memory (in hexadecimal)
print(N2)
#Accessing and printing Class's Attribute
print(N2.num)
Output
輸出量
<__main__.Number object at 0x7f96c06a0160>
123
<__main__.Number object at 0x7f96c06a06d8>
123
翻譯自: https://www.includehelp.com/python/example-to-define-a-class.aspx