Custom Python class __init__
function not properly generating variables or calling functions.
I have the following code which I wish to use to store and manipulate information about a mesh face. I'm running into issues where the __init__
function does not seem to do anything.
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)
When I run this code, I expect 2 print outputs. One from the print(self.light_sprite)
in the __init__
and one from the final line print(face.face_id)
.
>>> False
>>> 0
However instead I get the following error:
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'
Now when I replace the last two lines with these lines, suddenly face.face_id exists and is printable.
face = Face()
face.face_id = 0
print(face.face_id)
>>> 0
If I call one of the class functions, the class variables that are updated by the functions now suddenly exist.
face = Face()
print(face.color)
Traceback (most recent call last):
File "/Users/Documents/testing.py", line 46, in <module>
print(face.color)
AttributeError: 'Face' object has no attribute 'color'
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'
I feel like there is something basic that I'm missing, but I cannot see it. I would appreciate anyone who can help straighten me out as to why:
__init__
function is not running and calling the print command__init__
function is not initializing variables in the class instanceYour class has a typo. You have written __int__
instead of __init__
at your third line.
__int__
is a separate built-in method for converting the class itself into the integer type.
For example:
class Face:
# some init method
def __init__(self, value):
self.value = value
def __int__(self):
return int(self.value) + 1
f = Face("1")
print(int(f) + 1)
will print out
3