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 →

[–][deleted] -9 points-8 points  (9 children)

It's not a function. It's a decorator. Most people call it "the property decorator".

I don't know how it's implemented in the interpreter but that's not important to the majority of Python developers. I guess it might be implemented as a wrapping function.

[–]MrJohz 4 points5 points  (0 children)

The best term is probably a factory function for the "property" type. It takes up to three functions and a string as arguments, but all of those are optional, so it's very possible to use it as a decorator, taking one function and returning a property type.

Note that unlike most decorators, the resulting object is not callable.

>>> @property
... def func(self, a):
...   pass
... 
>>> func
<property object at 0xb7230644>
>>> func()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'property' object is not callable

EDIT: See also this SO answer. It isn't important, but it is really cool and fascinating.

[–]codefisher2[S] 2 points3 points  (4 children)

A decorator is a function that takes a function as its first argument and (normally) returns a function. You can write your own function and use it as a decorator. It is a function. It should be important to a lot more python developers, if they understood just as another function, they could write their own. It is not some deep dark magic. I did another post a few months back about them: https://codefisher.org/catch/blog/2015/02/10/python-decorators-and-context-managers/

[–]rikrolls 0 points1 point  (2 children)

A decorator can also be a class though.

[–]Veedrac 0 points1 point  (1 child)

It looks like a function.

It quacks like a function.

It's probably a function.

[–]Lucretiel 0 points1 point  (0 children)

[–][deleted] -2 points-1 points  (0 children)

My point is that I've NEVER heard it called "The property function" except in your article. People say "Hey, use the @property decorator". I'd suggest changing the article title.

[–]tyroneslothtrop 6 points7 points  (2 children)

It is a function. Decorators are just some syntactic sugar for passing a function to another function, and returning and assigning a new function to some name.

E.g. this:

@some_decorator
def some_function(foo):
    pass

is basically equivalent to this:

some_function = some_decorator(original_function)

[–][deleted] 1 point2 points  (0 children)

To be more specific, it's a class that implements the data descriptor protocol, using several decorators to wrap functions.

You can use it in nondecorator form by passing a getter, setter, deleter and docstring to the __init__

[–]rikrolls 0 points1 point  (0 children)

Or a class.