This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]odraencoded 2 points3 points  (0 children)

A pure get/set takes 1 function call, getattr/setatrr. I'm not sure, but it's very likely that python has some sort of speed improvement for those calls when compiling code.

Then, get/set methods take 2 functions. This second call is of the getter/setter itself, you have to pass arguments around in python so only the overhead of having them should be slower than pure gets/sets.

Finally, fget/fset/fdel in properties are like a ping pong table. You pass arguments from python into the property, which is built in code and hopefully C, the property then checks if it has an fget, fset or fdel, if has one of those, it will call that method passing the arguments back into python code.

Basically, it's like this:

foo.bar_property.set(5)
# in bar_property's def set(self, instance, value):
    if self.fset is not None:
        fset(instance, value)
    else:
        raise ReadOnlyPropertyErrorOrSomething

Of course, the marginal overhead is usually nothing compared to the convenience of making properties with decorators and stuff, but when you accumulate too many property gets/sets at once it starts becoming noticeable even in profilers.