Trouble with property

O

Odalrick

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
 
G

Gabriel Genellina

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
 
O

Odalrick

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.

/Odalrick
 
B

Ben Finney

Odalrick said:
Gabriel Genellina skrev:


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,108
Latest member
Virus9283

Latest Threads

Top