Use of @ (at sign) * in code listings

Hello,
I am a total beginner and I am confused by the use of asterisks in books that I am using. For example this code from a beginners book.

@dataclass
class Product:
    # three attributes with default values
    name:str = ""                                 # attribute 1
    price:float = 0.0                             # attribute 2
    discountPercent:float = 0              # attribute 3

I would appreciate somebody explaining this.
Sean43

I don’t see any asterisks in the code snippet you posted. . .

It is greyed out in this line.
@dataclass

Do you mean the at sign (@)? (An asterisk is *.)

The at sign is used for decorators. If you search online for “Python decorators” you can find many tutorials and plenty of info.

Thank you for your help. No wonder I was confused when I couldn’t tell the difference between an at sign and an asterisk.
And thank you for your time and courtesy.
Sean43

Topic titles can be edited.

The doc index symbols page has several entries for ‘@’.

Hi,

there is nothing ‘beginner’ with regards to the topic of decorators. In fact, this is considered a rather advanced topic. If you are just starting out, I would steer clear from this topic as it can get confusing really fast as there are many forms that they can take (from built-ins to user defined decorators - function decorators / class decorators, etc.). This is generally covered well after you have covered object oriented programming (OOP) which is another huge topic onto itself. Stick to learning the fundamentals first: loops, iterables, lists, functions, parameters, dictionaries, conditional statements, scope, importing, etc. Once you have covered these topics at a minimum, then you will be in a better position to understand decorators.

Cheers!

Thank you. That seems to be very good advice.
Sean

Thank you. That page seems to chock full of information.

Defining them can be complex, but it’s usually safe to use them. Particularly if you get into web frameworks and such, you’ll likely be making good use of decorators that other people have created.

1 Like

Right now I am actually covering this very topic in depth. So many versions possible. I am sure it will make more sense once I actually start applying them to something practical and tangible as opposed to just going over the theory and countless examples.

Thank you.