banner



Can We Define Decorator In Class

Method decorators in a class

Image Courtesy of Roselyn Tirado via Unsplash

Decorating Methods

Decorators provide a convenient and elegant manner to add functionality to functions and methods in Python. Decorators can be implemented in a number of different ways. One useful utilise-case for decorators involves using them with methods defined in a course. Decorating methods in the classes we create tin can extend the functionality of the defined method. For example, we could implement a data integrity check, or write the output of the method telephone call to a file. It really is up to the states what we cull to exercise. Method decorators simply provide us with the facility to extend functionality in an elegant way.

Every bit always, the use of decorators with methods will be best demonstrated using an instance.

In the case below, I accept created a simple course called NumericalOps, which requires 2 arguments, val1, and val2. In the init constructor, these arguments get ready as attributes in the object. Next, I accept 2 methods divers in this grade, namely, multiply_together and ability.

Lets proposed the rather niggling and contrived scenario. I would like the two attributes, val1 and val2 set in the object to be numerical integers. The multiply_together method simply multiplies the two values together and returns the value from this operation. Importantly, in Python, it is possible to multiple a string, for example by an integer. This is demonstrated when nosotros create an case of the NumericalOps class, ten, and pass in the two arguments, 2 and 'my_string', as shown in the console output in the example below.

To provide information input integrity, a decorator can exist used to decorate a method defined in the class. Currently, in the case, the decorator, @integer_check is commented out.

To provide information integrity, I volition write a decorator, named interger_check. To decorate a method in a class, outset employ the '@' symbol followed by the proper noun of the decorator function.

A decorator is simply a office that takes a function as an argument and returns nevertheless another function. Here, when we decorate, multiply_together with integer_check, the integer part gets chosen. The multiply_together method gets passed equally the statement to the integer_check function and returns inner. The local inner function and so gets chosen, which takes the object which we call ref, and checks the attributes using the isinstance function. If a not-integer type gets passed, for case, 'my_string', a TypeError exception gets raised, and a message is the output to the console instructing the user where they went wrong. If both arguments are withal integers, inner returns the original method with the object passed as an argument.

The source code for this case can exist institute on my GitHub page, here and below.

Using the Same Decorator for Multiple Methods

To increase both the usability and the utility of decorators, we tin design their implementation to be used with multiple methods.

Once more, this is best illustrated with an case, so allow's build on the example from the previous section.

Let's advise, that I would also like the integer_check decorator to validate that the exponent nosotros use in the power method is also an integer. That makes logical sense, considering when we phone call the power method, it simply calls the multiply_together method, and multiplies the return value of this method past the exponent. In each instance, therefore, I need to make sure that val1, val2 and the exponent are integers.

To implement this behaviour, I brand an alteration to the integer_check decorator. I simply gear up a default argument to the expo parameter in the local inner function defined inside the decorator. Now, if we call the power method on our case, y, inner will be returned and then subsequently called with both the object which we notate in the inner definition as ref, and the expo value.

In the conditional block, if expo is passed, nosotros cheque its blazon using the isinstance function, heighten an fault if it is non an integer, or return the method, with the object and the exponent if the exponent and val1 and val2 are all integers.

Usefully, we tin still use the integer_check decorator on the multiply_together method, because a default value of None has been set for expo.

Now, we have a decorator which can perform user input validation beyond multiple methods in our class.

With integers as input, nosotros go the expected behaviour in our console output.

With erroneous input (not-integer types), we go a helpful instructive error message.

The source code for this instance can be found hither.

Summary

Of class, as always, the examples presented are rather little and contrived. There are other ways to implement the behaviour seen hither, without the requirement for decorators. My intention here is to demonstrate how decorators work, and how they could exist incorporated into your code pattern. For those interested, I take besides written another two tutorials detailing how decorators can be used to decorate functions, and how the decorator itself can exist a class. The links to these ii articles can be establish by accessing the links.

Source: https://towardsdatascience.com/decorating-methods-defined-in-a-class-with-python-4b361589440

Posted by: tannenbaumragretheable1957.blogspot.com

0 Response to "Can We Define Decorator In Class"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel