0

I'm writing a driver in Python 2.6 and I need it to be reverse compatible with a previous implementation (I don't have access to the source code).

class new_driver ():

     def output(self, state):

          if state == True:
              self.set_ouput_on()
          else:
              self.set_output_off()

              ...

The odd thing is that to keep compatibility I have to pass this output using the format

nd = new_driver()
nd.output = True

How do I pass a value in this way?

Edit: To clarify: my "output" function has to receive the value True in this way in order to execute the function self.set_output_on()

2
  • So, your recipient expects nd.output to be boolean or what? Commented May 28, 2014 at 18:59
  • Why 2.6? And where do you want to pass it? Commented May 28, 2014 at 18:59

1 Answer 1

4

Try using the @property decorator:

@property
def output(self):
    return self... #not sure how you are tracking output on/off

@output.setter
def output(self, state):
    if state:
        self.set_output_on()
    else:
        self.set_output_off()
Sign up to request clarification or add additional context in comments.

1 Comment

I did a fair bit of research on decorators (I'm a python noob) but I don't really understand what the build in property function is/does. Also I understand the possible use of @output but am confused about what .setter is/does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.