paris_serviola
paris_serviola

Reputation: 439

pass two arguments in python property setter

class MyClass(object):
   def __init__(self, name):
     self.name = name
     self._prof = None
     self._ischanged = False

   @property 
   def prof(self):
     if not self._prof:
        self._prof = "Sample Prof"
     return self._prof

   @prof.setter
   def prof(self, value, anothervalue):
      pass

Here you can get the prof using:

myclass = MyClass("sam")

myclass.prof

but I can't find a way to set the values of prof.

In the above code, how would I pass two values to prof?

Upvotes: 4

Views: 6507

Answers (1)

mgilson
mgilson

Reputation: 309929

You can't pass two values to a setter. when you do:

instance.property = whatever

python calls the setter with instance (as self) and whatever as arguments.


You can pass a 2-tuple though:

myclass.prof = ('one', 'two')

In this case, the setter will get passed the 2-tuple ('one', 'two') as the value. If that isn't what you want, you're better off using a vanilla method to set the values. e.g.:

myclass.set_prof(value1, value2)

Upvotes: 8

Related Questions