Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class help
#1
Can anyone help with the code below. I was wondering whether the if statement should live within the class?. When I have tried this before I can't seem to find a way to print the status using the __str__ method

class Device:

    def __init__(self, dev_id, dev_status):
        self.dev_id = dev_id
        self.dev_status = dev_status

    def __str__(self):
        return f'{self.dev_id} - {self.dev_status}'


#output from monitoring device
output = 'DEVICE,281,3,4'

output_split = output.split(',')

#should/could this live within the class object?
if output_split[3] == '3':
    status = 'Active'
elif output_split[3] == '4':
    status = 'Inactive'

#here I was send output_split[3] rather than status
occupancy_sensor1 = Device(output_split[1], status)

print(occupancy_sensor1)
Reply


Messages In This Thread
Class help - by txrx - Dec-18-2022, 05:01 PM
RE: Class help - by txrx - Dec-18-2022, 05:09 PM
RE: Class help - by menator01 - Dec-18-2022, 05:31 PM
RE: Class help - by txrx - Dec-18-2022, 06:16 PM
RE: Class help - by deanhystad - Dec-18-2022, 06:26 PM
RE: Class help - by txrx - Dec-18-2022, 07:50 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020