Python自定义类__init__未初始化实例变量及函数未执行问题
问题描述
我编写了一个用于存储和处理网格面信息的Python自定义Face类,运行时遇到以下问题:
- 实例化
Face类后,调用print(face.face_id)触发AttributeError,提示对象无face_id属性; __init__函数内的print(self.light_sprite)语句未输出预期内容。
但手动为实例赋值face.face_id = 0后可正常打印,调用类方法set_srf_color后color属性也能正常访问。我希望搞清楚三个问题:
__init__函数为何未执行且未调用print命令?__init__为何未初始化类实例变量?- 手动设置或调用函数更新变量后,变量为何突然可用?
相关代码
class Face(): """A class to store and manipulate information about a face""" def __int__(self): self.face_id = 0 self.vertices=[0,1,2] self.color=[255,255,255] self.bright=False self.transparency=0 self.light_sprite=False self.normal_location=[0,0,0] self.normal_vector=[0,0,0] self.double_sided=False print(self.light_sprite) def set_srf_normal(self, data): """Set the normal information of the face from a SRF format""" self.normal_location = [float(i) for i in data[:3]] self.normal_vector = [float(i) for i in data[3:]] # Determine if face is double-sided if the normal vector is zero for all elements. double_sided = True for i in self.normal_vector: if i != 0: double_sided = False break self.double_sided = double_sided def set_srf_color(self, data): """Set the face color from a SRF format input""" self.color = [int(i) for i in data] # Valid colors component values are 0-255. for idx, color in enumerate(self.color): if color > 255: self.color[idx] = 255 elif color < 0: self.color[idx] = 0 def print_properties(self): print(self.face_id, self.vertices, self.color, self.normal_location, self.normal_vector, self.color, self.light_sprite, self.transparency, self.bright, self.double_sided) face = Face() print(face.face_id)
预期输出
False 0
实际报错
Traceback (most recent call last): File "/Users/Documents/testing.py", line 46, in <module> print(face.face_id) AttributeError: 'Face' object has no attribute 'face_id'
额外测试情况
- 手动赋值后可正常打印:
face = Face() face.face_id = 0 print(face.face_id)
输出:0
- 调用方法后对应属性可用,未初始化的属性仍报错:
face = Face() face.set_srf_color([0,255,0]) print(face.color) print(face.face_id)
输出:
[0, 255, 0] Traceback (most recent call last): File "/Users/Documents/testing.py", line 48, in <module> print(face.face_id) AttributeError: 'Face' object has no attribute 'face_id'
问题解答
1. __init__函数未执行的原因
你把构造函数的名字写错了!Python中类的构造函数必须是__init__(双下划线+init+双下划线),但你写成了__int__(把init误写为int)。这个错误导致Python无法识别这是构造函数,实例化对象时不会自动执行该方法,因此里面的print(self.light_sprite)语句不会触发输出。
2. __init__未初始化实例变量的原因
同样是构造函数名字错误导致的——__int__不是Python默认的构造函数,实例化Face对象时不会调用它,所以方法内定义的self.face_id、self.color等属性根本没有被创建,自然访问时会抛出属性不存在的错误。
3. 手动设置或调用函数后变量可用的原因
Python是动态语言,对象的属性可以在任何时候动态添加:
- 手动赋值
face.face_id = 0时,相当于给face实例新增了一个face_id属性,因此之后能正常访问; - 调用
set_srf_color方法时,方法内执行self.color = [...],这也是动态给实例添加color属性的操作,所以该属性能被访问,但其他未被手动添加或通过方法创建的属性(比如face_id)依然不存在,访问时仍会报错。
修复方法
将__int__修改为正确的__init__即可:
class Face(): """A class to store and manipulate information about a face""" def __init__(self): # 修正构造函数名称 self.face_id = 0 self.vertices=[0,1,2] self.color=[255,255,255] self.bright=False self.transparency=0 self.light_sprite=False self.normal_location=[0,0,0] self.normal_vector=[0,0,0] self.double_sided=False print(self.light_sprite) # 其他方法保持不变...
内容的提问来源于stack exchange,提问作者Decaff_42
相关产品推荐
相关产品推荐

