Trouble with property

Odalrick odalrick at hotmail.com
Mon Oct 23 19:28:06 EDT 2006


I'm having some trouble with using property. This class is supposed to
represent a rectangle that can never be outside an area.

I'll snip the parts I don't think are relevant. Or should I? I dunno.

class ConstrainedBox:
	def __init__( self, size, ratio ):
#              Various instance variables initialized
		self.reset( size )

	def reset( self, size ):
		self._width, self._height = size
		self._height_defining_dimension = self._width > self._height *
self.ratio
		make_log( ' _height_defining_dimension =' + str(
self._height_defining_dimension ) )
		if self._height_defining_dimension:
			self._max_linear_size = self._height
# This, and the corresponding line in the else clause is what is
causing trouble.
# self.linear_size is, as far as I can tell, the property I define last
in the class
# If I replace it with _set_linear_size, which it is supposed to call,
everything works
			self.linear_size = self._height
#
		else:
			self._max_linear_size = self._width
			self.linear_size = self._width
		self.center_box( self._width / 2, self._height / 2 )
# Lots of methods snipped
	def _set_linear_size( self, value ):
		if value >= self._min_linear_size:
			if value <= self._max_linear_size:
				self._linear_size = value
			else:
				self._linear_size = self._max_linear_size
		else:
			self._linear_size = self._min_linear_size
		self._scale_box()
# Some more snippage
# The property that is causing grief
	linear_size = property( _get_linear_size, _set_linear_size )
	rectangle = property( _get_rectangle )
	centre = property( _get_centre, _set_centre )


By adding some logging I found out that _set_linear_size never is
called, so the rectangle remains the default size forever.

/Oldarick




More information about the Python-list mailing list