I just started Python OOP and i also have basically 0 knowledge of python programming but we were required to build a simple class and function and i dont understand why in one case it works and in the other it doesnt
class Digital_signal_information:
def __init__(self, signal_power :float, noise_power :float, n_bit_mod:int):
self.signal_power=signal_power
self.noise_power=noise_power
self.n_bit_mod=n_bit_mod
class Line:
def __init__(self,loss_coefficient:float, length:int):
self.loss_coefficient=loss_coefficient
self.length=length
def Loss(self,loss):
self.loss=loss_coefficient*length
BPSK=Digital_signal_information(0.001, 0, 1)
QPSK = Digital_signal_information(0.001, 0, 2) #basically in these 4 cases i have no problem
Eight_QAM = Digital_signal_information(0.001, 0, 3)
Sixteen_QAM = Digital_signal_information(0.001, 0, 4)
#but if i do
a=Line(1.0,2);
#and when i try to see if i can call a.Loss it shows nothing
As i said i really just started on OOP in python so im quite confused as to whats wrong
In the exercise we have to create the class Line
where we have to use the attributes signal_power
and length
and we have to use a function (property) (correct me if im wrong im still getting used to the OOP vocabulary). But as i said im quite confused as to why it works in one part and doesnt in the other
>Solution :
You can use @property
so that the return value of the function can be directly accessed as a.Loss
.
@property
def Loss(self):
return self.loss_coefficient * self.length
# ...
a = Line(1.0,2)
print(a.Loss) # 2.0