Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #108865 > unrolled thread

How do I subclass the @property setter method?

Started byChristopher Reimer <christopher_reimer@icloud.com>
First post2016-05-20 11:50 -0700
Last post2016-05-20 11:50 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  How do I subclass the @property setter method? Christopher Reimer <christopher_reimer@icloud.com> - 2016-05-20 11:50 -0700

#108865 — How do I subclass the @property setter method?

FromChristopher Reimer <christopher_reimer@icloud.com>
Date2016-05-20 11:50 -0700
SubjectHow do I subclass the @property setter method?
Message-ID<mailman.55.1463773847.27390.python-list@python.org>
Greetings,

My chess engine has a Piece class with the following methods that use 
the @property decorator to read and write the position value.

     @property
     def position(self):
         return self._position

     @position.setter
     def position(self, position):
         if self._first_move:
             self._first_move = False
         self._position = position

Blank is a subclass of the Piece class that represents an empty space on 
the board and  is a placeholder in the Board class _state dict. Since 
Blank is a placeholder and not a playable game piece, I want to make 
@position.setter non-operational (i.e, make no changes to the position 
value).

     @Piece.position.setter
     def position(self, position):
         pass

This code works and the corresponding unit test blows up because I 
haven't changed it yet, but the PyCharm IDE complains that the 
Blank.position signature doesn't match the Piece.position signature. 
Never mind that I copy and paste the identical declaration from the 
Piece class.

This code does work, blows up the unit test, and keeps PyCharm happy.

     @property
     def position(self):
         return super().position

     @position.setter
     def position(self, position):
         pass

Re-declaring @property and calling super seems redundant.  Not sure if I 
found a bug with the PyCharm hint feature or I'm not subclassing the 
@property setter correctly. Which is it?

Thank you,

Chris R.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web