What are the differences between Decorators and Attributes?

Asked

Viewed 102 times

7

I asked a question recently about the use of attributes that was introduced in PHP 8. Attributes to me end up not sounding like novelty, because I remember seeing something like that when I worked with C#.

I even remember that the syntax is not very different from PHP:

[Serializable]
public class SampleClass
{
    // Objects of this type can be serialized.
}

But a certain doubt arose when I remember that I saw something similar in Python. I know that in Python it is called decorators.

@app.route('/')
 def home(response):
     return template(response, 'meu_template.html')

I remember doing a question concerning the syntax of C#, but I wonder if I didn’t confuse between Decorator and Attributes today.

My doubts are:

  • Decorators and Attributes are/do the same thing? Python decorators for example have the same purpose as C# and PHP?
  • If they are not the same thing, what are the differences between them?
  • Are there languages other than Python that use the term "decorators"? Is this a standard language term or is it familiar in programming?
  • Terms "decorators" and "attributes" may have other names depending on the programming language used?
  • 2

    As far as I can see, there is an important practical difference, some just add extra information to the point (class, method, function, etc.) where they are defined, so, by themselves, they do nothing, they need some means of reflection to be used. While others are always executed when defined, they don’t need anything other than themselves (not that they can’t have), so they are (at least the ones I know) a function. Now, what term is used for each one, I can’t say, nor how it works in each language, in Typescript, I know that it is 2°mode, probably by necessity

1 answer

4


Certain terms may be used in slightly different contexts and may have large or small differences in interpretation.

About PHP I replied What are attributes in PHP? (Annotations/Attributes/Decorators).

Usually the attribute (really, not the confusion that people make) is a mechanism often used to achieve the goal of the so-called Decorator Pattern. Other mechanisms can be used, even because some languages do not have their own forms.

One way to decorate something is to encapsulate a function into another, so instead of directly calling the function you want, it calls another that "prepares the ground" and eventually finishes the main function.

When using attribute you usually need some mechanism to automate this and do something extra, usually through reflection, at runtime or at compile time. It gets a cleaner syntax and can be less confusing when one knows what one does.

A language can call the attribute of other names, such as annotation (Java) or even decorator, as seen above.

The exact way it works may be different in each language, but the essence is the same. My understanding is that Python, C# and PHP have the same mechanisms, although they may have small differences of functioning.

I can’t remember all the details of all the languages to talk about their terms, I remember seeing the use of decorator only in Python.

I don’t even know the details of various languages. I know that in Python the decorator is effectively a function. In C# is a class and Java too, although more difficult to do, and it seems that PHP copied the same. It may be that the biggest differences are in the reflection methods to access the attributes and change the behavior or configure how a function should be executed. I think it would even be broad to talk about it because it would be many languages and many details.

Interestingly in C# I see a lot of people using attributes in a very descriptive way, but it can be much more than that, the class that creates an attribute can have virtually any code. It is true that most of the time it is not necessary to have, but when it can be useful people forget or do not know that can.

I wish there were decorators only at compile time, because most are only used in this case, and if the language has reflection at compile time (C# now has), it hardly needs any other way. C# has proposed to have these decorators that do not go to the executable.

I find it curious when a language adopts a mechanism very late, and so it can do better because it has learned from the mistakes of the pioneers, and it makes the same mistakes, even worse when other languages are fixing. Then I say language doesn’t evolve well, people think I am hater language, I deal with facts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.