定义类的Python示例
生活随笔
收集整理的這篇文章主要介紹了
定义类的Python示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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():#Attributenum = 123# main code if __name__ == "__main__": # creating first object N1 = Number()#Printing object's memory (in hexadecimal)print(N1) #Accessing and printing Class's Attributeprint(N1.num) # creating first object N2 = Number()#Printing object's memory (in hexadecimal)print(N2) #Accessing and printing Class's Attributeprint(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
總結
以上是生活随笔為你收集整理的定义类的Python示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小米5c多少钱啊?
- 下一篇: chdir函数_PHP chdir()函