osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

newb question about @property


Stephan Houben wrote:
> Op 2017-10-01, Bill schreef <BILL_NOSPAM at whoknows.net>:
>> I watched an example on YouTube where someone wrote a simple descriptor
>> ("@Time_it) to output the amount of time that it took ordinary functions
>> to complete.    To be honest, I AM interested in descriptors.
> Are you sure you are not confusing deSCRIPTtors and deCORAtors here?

Yet, you are absolutely correct!  Thank you for clarifying! From your 
description, I can see that it was *decorators*, which drew my 
interest.  It appears that *property* is perhaps both a decorator and a 
descriptor, at least when used as in the examples we have been 
discussing.  According to the language grammar, which all newbys like me 
should have handy (remember my ADT acronym from another thread?), 
decorators can be applied to a classdef, a funcdef, or a async_funcdef 
(the latter I assume is a "callback" function definition).   Surely the 
difference in syntax between funcdef and async_funcdef will be revealed 
to me by looking closer at the grammar! : )

Bill

>
> @Time_it
>
> is decorator syntax.
>
> Despite the similarity in the words, there are totally different things.
>
> Descriptors are objects with __get__, and optionally __set__ and
> __delete__ methods (i.e. they implement the descriptor protocols).
>
> Decorators aren't really an official type, but loosely speaking these
> are any functions which can be applied meaningfully with a single
> function or class as argument. Some very mundane functions can be
> (ab)used as decorators.
>
> In [1]: @repr
>     ...: def hello():
>     ...:     pass
>     ...:
>
> In [2]: hello
> Out[2]: '<function hello at 0x1048a50d0>'
>
> Stephan