Pie Syntax (@) in Python


The Pi method or Pi Syntax is used to decorate a function or method in Python, and is known as a decorator. The pie method is used to modify the behaviour of a function or method.

Pie Syntax (@) can be used by simply placing the @ symbol and the name of the decorator function above the definition of the function. When the function is invoked, Python is instructed to use the decorator.

Pie Syntax (@) has the benefit of allowing for more adaptability in function calls. When working with complicated data structures or when you need to put in a variable number of arguments, the Pie Syntax (@) allows you to pass in many parameters as a single argument. Your code will become more flexible and situation-specific as a result.

Syntax

@<my_decorator>

Method 1: Using Function Decorator

Example 1

def my_decor(fun):
   print("Welcome")
   fun()
   print("Tutorialspoint")

@my_decor
def main():
   print("to")

if __name__ == 'main':
   main()

Output

Welcome
to
Tutorialspoint

In this code, a decorator function named my_decor has been created. This function inherits and handles another function called fun .

In this decorator function, a welcome message is printed by first typing "Welcome". Then fun() is called, which runs the fun function. Finally "Tutorialspoint" is printed.

In the above code, a function called main is decorated by the decorator my_decor. So, the main function is passed to my_decor and the my_decor function receives and handles it. The welcome message is printed by the my_decor function, then the main function is called, and finally "Tutorialspoint" is printed.

If __name__ will be checked to see if the code is only running as the main program. __name__ will be equal to "main" when main is the program, so the main function is called.

Example 2

def my_decor1(fun):   # 1st decorator 
   print("---reading---")

def my_decor2(fun):   # 2nd decorator
   print("---Visit---")
   fun()
   print("---for---")

@my_decor1
@my_decor2
def main():
   print("---Tutorialspoint---")

if __name__ == 'main':
   main()

Output

---Visit---
---Tutorialspoint---
---for---
---reading---

In this program, we provide three different functions: my_decor1, my_decor2, and main. These works are specially used for decoration.

The first function is my_decor1, in which we print the message " ---reading---". It runs before the second function my_decor2.

The second function is my_decor2, in which we print the message " ---Visit---". Then we call fun() , which will call main . main's print statement "---Tutorialspoint---" runs. Then we print " ---for---".

The main function is decorated in two ways. The first decor belongs to my_decor2, and the second decor belongs to my_decor1. It means that to decorate the main first my_decor2 function will run and then my_decor1 function will run. In the end the result will be printed.

Method 2: By Using Class Method Decorator

Example 1

class Subject:
   count = 0

   def __init__(self, sub):
      self.sub = sub
      Subject.count += 1

   @classmethod      #class method decorator(@)
   def get_count(dec):
      return dec.count

Subject1 = Subject("Python")
Subject2 = Subject("DBMS")
Subject3 = Subject("HTML")

print(Subject.get_count())

Output

3

In this code we have a class called "Subject" which has a local variable "count". There is a method called __init__() which is called when an object is created. This method sets the member variable "sub" returned by the two methods and increments "Subject.count" by 1.

There is a class method called get_count() which returns the member variable "count" of the "Subject" class. Next, we create three "Subject" objects (Subject1, Subject2, Subject3) and assign them with "Python", "DBMS" and "HTML" member variables. We then print "Subject.get_count()" which will give us the output of "3".

Example 2

class Team:
   count = 0

   def __init__(self, tm):
      self.tm = tm
      Team.count += 1

   @classmethod     #class method decorator(@)
   def get_count(team):
      return team.count

Team_1 = Team("CSK")
Team_2 = Team("KKR")
Team_3 = Team("RCB")
Team_4 = Team("LSG")

print(Team.get_count())

Output

4

In this code, we have created a class named 'Team'. This class has a spatial number named 'count' which is initialized to 0. In addition, this class has an internal method called 'init' which takes a spatial parameter called 'tm' and stores them in 'self.tm'. Additionally, this method is used to increment 'Team.count'.

Next, we have created a '@classmethod' called 'get_count' that takes a spatial parameter called 'team' as a parameter. This method returns 'team.count'. In the next step, we have created four combinations from 'Team_1' to 'Team_4', where each combination has its own name ('CSK', 'KKR', 'RCB', 'LSG' in that order).

Finally, we have used 'print(Team.get_count())' which calls the 'get_count' method used by the 'Team' class. This gives us the total number of combinations created in the class 'Team'.

Conclusion

In conclusion, the (@) syntax in Python is that it is a decorator used to modify or modify the behaviour of a function. It allows a function to be edited by another function. (@) Decorator is used by the user to change the default behaviour, change the behaviour of the class, logging, caching etc. It can be a class, argument, function, or user defined decorator decorated using a decorator with the @ sign above the function.

Updated on: 29-Sep-2023

123 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements