By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 1,987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

to setattr or def setProp()

P: n/a
I have a class for rectangle and it has two points in its __slots__ .
However, we can derive a number of properties like width, height,
centerPoint etc from these two points. Now, I want to be able to set
and get these properties directly from the instances. I can either def
__setattr__ , __getattr__ or I can define function for each property
like setWidth(), setHeight() . Which one would be faster? I would
prefer doing through __setattr__, __getattr__ because it makes the code
more readable.
However, speed is a concern I would have to do these operations in few
thousand iterations for about 50-100 rectangles.

--
Suresh

Nov 14 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In <11**********************@b28g2000cwb.googlegroups .com>,
jm*******@no.spam.gmail.com wrote:
I have a class for rectangle and it has two points in its __slots__ .
However, we can derive a number of properties like width, height,
centerPoint etc from these two points. Now, I want to be able to set
and get these properties directly from the instances. I can either def
__setattr__ , __getattr__ or I can define function for each property
like setWidth(), setHeight() .
Or you can use `property()` to create "computed attributes".

Ciao,
Marc 'BlackJack' Rintsch
Nov 14 '06 #2

P: n/a
On Mon, 13 Nov 2006 22:20:03 -0800, jm*******@no.spam.gmail.com wrote:
I have a class for rectangle and it has two points in its __slots__ .
However, we can derive a number of properties like width, height,
centerPoint etc from these two points. Now, I want to be able to set
and get these properties directly from the instances. I can either def
__setattr__ , __getattr__ or I can define function for each property
like setWidth(), setHeight() . Which one would be faster? I would
prefer doing through __setattr__, __getattr__ because it makes the code
more readable.
However, speed is a concern I would have to do these operations in few
thousand iterations for about 50-100 rectangles.
>>class Rect(object):
.... def __init__(self, a,b,c,d):
.... # too lazy to use slots, sorry
.... self.a, self.b, self.c, self.d = a, b, c, d
.... def _width(self):
.... return self.c - self.a
.... width = property(_width, None, None, None)
....
>>>
r = Rect(1, 2, 3, 4)
r.width
2

Now let's see how fast it runs.
>>import timeit
timeit.Timer("r.width", "from __main__ import Rect; r = Rect(1,2,3,4)").timeit()
2.3775210380554199

Now you know how to test how fast code runs, you can write some code to
test.
--
Steven D'Aprano

Nov 14 '06 #3

P: n/a
jm*******@no.spam.gmail.com wrote:
I have a class for rectangle and it has two points in its __slots__ .
However, we can derive a number of properties like width, height,
centerPoint etc from these two points. Now, I want to be able to set
and get these properties directly from the instances. I can either def
__setattr__ , __getattr__
But this won't help with introspection...
or I can define function for each property
like setWidth(), setHeight() . Which one would be faster?
You mean faster performances or faster to implement ?
I would
prefer doing through __setattr__, __getattr__ because it makes the code
more readable.
You mean the client code ?
However, speed is a concern I would have to do these operations in few
thousand iterations for about 50-100 rectangles.
The canonical solution is to use either properties or custom
descriptors[1]. Custom descriptors should be a bit faster than
properties (since you save a couple function calls), but I have not done
any benchmark... It should also be a bit faster than __getattr__, which
is only used as a fallback when all other lookup rules have failed (here
again, don't take me on words and do some benchmarking). As a last
point, descriptors show up as class and instance attributes, which helps
with introspection.
class Height(object):
def __get__(self, instance, cls):
if instance is None: return self
return instance.bottomright.y - instance.topleft.y

def __set__(self, instance, value):
instance.bottomright.y = instance.topleft.y + value

class Width(object):
def __get__(self, instance, cls):
if instance is None: return self
return instance.bottomright.x - instance.topleft.x

def __set__(self, instance, value):
instance.bottomright.x = instance.topleft.x + value

class Rectangle(object):
def __init__(self):
self.topleft = Point(10, 10)
self.bottomright = Point(20, 30)

height = Height()
width = Width()

r = Rectangle()
r.with = 40

(not tested)

[1] :
* http://docs.python.org/ref/descriptors.html
* http://users.rcn.com/python/download/Descriptor.htm
My 2 cents...
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Nov 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.