471,853 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,853 software developers and data experts.

Trouble with property

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

Oct 23 '06 #1
3 946
At Monday 23/10/2006 20:28, Odalrick wrote:
>I'm having some trouble with using property. This class is supposed to
represent a rectangle that can never be outside an area.

class ConstrainedBox:
# The property that is causing grief
linear_size = property( _get_linear_size, _set_linear_size )

By adding some logging I found out that _set_linear_size never is
called, so the rectangle remains the default size forever.
You must use a new-style class for properties to work. That is: class
ConstrainedBox(object):

--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
Oct 24 '06 #2

Gabriel Genellina skrev:
You must use a new-style class for properties to work. That is: class
ConstrainedBox(object):

--
Gabriel Genellina
Softlab SRL
Thank you, I've seen it mentioned, but never knew what it meant.

/Odalrick

Oct 24 '06 #3
"Odalrick" <od******@hotmail.comwrites:
Gabriel Genellina skrev:
You must use a new-style class for properties to work. That is:
class ConstrainedBox(object):

Thank you, I've seen it mentioned, but never knew what it meant.
More generally, a "new-style class" is important in this instance
because it inherits, directly or indirectly, from a built-in type,
becoming part of the main class hierarchy.

There are very few reasons to use old-style classes these days (and I
don't know what those reasons are anymore). Avoid them unless you know
you want them for some specific reason.

--
\ "I got some new underwear the other day. Well, new to me." -- |
`\ Emo Philips |
_o__) |
Ben Finney

Oct 24 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.