{"id":41886,"date":"2023-02-27T05:48:27","date_gmt":"2023-02-27T05:48:27","guid":{"rendered":"https:\/\/www.askpython.com\/?p=41886"},"modified":"2023-02-27T05:48:28","modified_gmt":"2023-02-27T05:48:28","slug":"at-symbol-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/oops\/at-symbol-in-python","title":{"rendered":"What does the &#8220;at&#8221; (@) symbol do in Python?"},"content":{"rendered":"\n<p>Python is an excellent interpreted programming language and has its own syntax. The set of guidelines that specify how a Programming language will be written and executed is known as the syntax (by both the runtime system of the computer and by human developers). Perl, C, and Java are all closely related to Python in syntax. There are some definite distinctions between the languages, though. High readability was a priority when creating Python. <\/p>\n\n\n\n<p>In this article, let us try to understand the multiple uses of the &#8216;@&#8217; (at) symbol in Python along with its implementation. <\/p>\n\n\n\n<p>The two major use of the &#8220;@&#8221; symbol in Python language are as follow:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>As decorator<\/li>\n\n\n\n<li>For matrix multiplication (as a binary operator)<\/li>\n<\/ul>\n\n\n\n<p>Before learning the use of &#8220;@&#8221; as a decorator, let&#8217;s first understand what is a decorator and what is it used for.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a decorator?<\/h2>\n\n\n\n<p>A function called a decorator accepts another function as input, modifies it by adding some features, and then returns the updated function. Without changing the original function&#8217;s source code, one can do this. <\/p>\n\n\n\n<p>One can easily extend the functionality of the current functions by decorating them. By including that code inside the wrapper function, we may do this. Therefore, one can simply add or change any code they want inside the wrapper function without changing the original method in any way.<\/p>\n\n\n\n<p>To learn more about decorators <a href=\"https:\/\/www.askpython.com\/python\/examples\/decorators-in-python\" target=\"_blank\" rel=\"noreferrer noopener\">click here.<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example of decorator without using &#8220;@&#8221;<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#defining function with the wrapper function\ndef func1(func):\n  def wrapper():\n    print(&quot;Begin&quot;)\n    func()\n    print(&quot;End&quot;)\n\n  return wrapper\n\n#defining the function that is passed as parameter\ndef func2():\n  print(&quot;AskPython&quot;)\n\n#calling the function\nx = func1(func2)\nx()\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"951\" height=\"385\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/Decorator-example-without-using-@-symbol.png\" alt=\"Decorator Example Without Using @ Symbol\" class=\"wp-image-42986\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/Decorator-example-without-using-@-symbol.png 951w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/Decorator-example-without-using-@-symbol-300x121.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/Decorator-example-without-using-@-symbol-768x311.png 768w\" sizes=\"auto, (max-width: 951px) 100vw, 951px\" \/><figcaption class=\"wp-element-caption\">Decorator Example Without Using @ Symbol<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Use of &#8216;@&#8217; for decorating in Python<\/h2>\n\n\n\n<p>Now that we have an idea of what a decorator is, let&#8217;s understand the use of the &#8216;@&#8217; symbol in decorating. <\/p>\n\n\n\n<p>Take a look at the above example, we have assigned a variable x which is equal to the first function (func1) with the second function (func2) passed as its parameter. <\/p>\n\n\n\n<p>Then we call the variable to run the functions in the required way. Instead of working this way, the easy way is to add a line of code with the&#8221;@&#8221; symbol followed by name of the first function to it before starting with the second function. This works the same way as the above function. The benefit of using the &#8220;@&#8221; symbol method is that it makes code more readable and easier to understand.<\/p>\n\n\n\n<p> The example below will give a clear idea about the same.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#defining function with the wrapper function\ndef func1(func):\n  def wrapper():\n    print(&quot;Begin&quot;)\n    func()\n    print(&quot;End&quot;)\n\n  return wrapper\n\n#defining the function that is passed as parameter\n@func1\ndef func2():\n  print(&quot;AskPython&quot;)\n\n#calling the function\nfunc2()\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"366\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/Decorator-example-using-@-symbol-1024x366.png\" alt=\"Decorator Example Using @ Symbol\" class=\"wp-image-42987\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/Decorator-example-using-@-symbol-1024x366.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/Decorator-example-using-@-symbol-300x107.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/Decorator-example-using-@-symbol-768x274.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/Decorator-example-using-@-symbol.png 1033w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Decorator Example Using @ Symbol<\/figcaption><\/figure>\n\n\n\n<p>There are several decorators users will encounter, but the most prevalent ones include<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>@property:<\/strong> It is possible to access a method like a regular attribute by tagging it with @property decorator.<\/li>\n\n\n\n<li><strong>@classmethod: <\/strong>When you require a method that includes the class but isn&#8217;t instance-specific, a class method can be helpful.<\/li>\n\n\n\n<li><strong>@staticmethod:<\/strong> A static method is associated with the class rather than the class instance.&nbsp; The main distinction is that a static method doesn&#8217;t change the class in any way.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Use of &#8216;@&#8217; for matrix multiplication <\/h2>\n\n\n\n<p>In Python 3.5, the @ operator can be overloaded. &nbsp;It is termed __<strong>matmul<\/strong>__ since it is intended to perform matrix multiplication. To learn more about the same <a href=\"https:\/\/peps.python.org\/pep-0465\/\" target=\"_blank\" rel=\"noreferrer noopener\">click here. <\/a><\/p>\n\n\n\n<p>Below is the simple implementation of this method in Python programming language.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass example(list):\n  def __matmul__(self, y):\n    x = self\n    return (&#x5B;&#x5B;sum(x&#x5B;i]&#x5B;k] * y&#x5B;k]&#x5B;j] for k in range(len(y)))\n            for j in range(len(y&#x5B;0])) ] for i in range(len(x))])\n\nx = example(&#x5B;&#x5B;1, 5],&#x5B;2, 4]])\ny = example(&#x5B;&#x5B;3, 3],&#x5B;4, 2]])\n\nprint(&quot;OUTPUT: &quot;)\nprint(x @ y)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1020\" height=\"295\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/Matrix-Multiplication-using-@.png\" alt=\"Matrix Multiplication Using @ symbol\" class=\"wp-image-42815\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/Matrix-Multiplication-using-@.png 1020w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/Matrix-Multiplication-using-@-300x87.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/Matrix-Multiplication-using-@-768x222.png 768w\" sizes=\"auto, (max-width: 1020px) 100vw, 1020px\" \/><figcaption class=\"wp-element-caption\">Matrix Multiplication Using @ symbol<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In this article, we tried to comprehend the different uses of the &#8220;@&#8221; symbol. along with this, we also understood decorator, its uses, and benefits in the python programming language. To learn from more such easy to understand and detailed articles on various topics related to the Python Programming Language, check our <a href=\"https:\/\/www.askpython.com\/python-course-for-beginners\" data-type=\"page\" data-id=\"18525\">courses section<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python is an excellent interpreted programming language and has its own syntax. The set of guidelines that specify how a Programming language will be written and executed is known as the syntax (by both the runtime system of the computer and by human developers). Perl, C, and Java are all closely related to Python in [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":43002,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-41886","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oops"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/41886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=41886"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/41886\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/43002"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=41886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=41886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=41886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}