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