I'm having trouble with converting boolean values into a string 'Yes' or 'No'.
class Worker():
def __init__(self,name,duty):
self._name = name
self._duty = duty
@property
def name(self):
return self._name
@property
def duty(self):
return self._duty
@duty.setter
def duty(self, duty):
if duty == True:
return f'Yes' if self._duty else 'No'
def __str__(self):
return f'Name: {self._name} Duty: {self._duty}'
if __name__ == '__main__':
w1 = Worker('John', True)
print(w1)
I want the desired ouput to be Name: John Duty: Yes However, right now im getting Name: John Duty: True
CodePudding user response:
Duty setter is not getting called. I modified the code to work on my machine.
class Worker():
def __init__(self,name,duty):
self._name = name
self._duty = duty
self.duty = duty
@property
def name(self):
return self._name
@property
def duty(self):
print("This was never called")
return self._duty
@duty.setter
def duty(self, duty_):
if duty_:
self._duty = 'Yes'
else:
self._duty = 'No'
return self._duty
def __str__(self):
print(self.duty)
return f'Name: {self._name} Duty: {self._duty}'
if __name__ == '__main__':
w1 = Worker('John', True)
print(w1)
Answer I got was:
This was never called
Yes
Name: John Duty: Yes