Back

Sanskar

Keen Learner and Exp... • 1d

Day 24 of learning python as a beginner. Topic: decorators Somebody has suggested me that I should focus on some of the important things and he gave me a list of topics to learn. Decorators were at very top of that list and therefore I decided to learn what decorators really are in python. A decorator is a function that helps us to expand and modify another function without changing its source code for example in my case I wanted that user is able to see the time at which he performed an arithmetic calculation. I had two ways of achieving this first the most simple and basic: is to just use the same line everywhere or make a function of this and call it just before the calculations. However this may not be the very efficient way because it may cause repetition of same lines which may unnecessarily increase the line of code and may not be very readable. This is where decorators come to save is you just need to create a function then wrap it by using functools's wrap function () to preserve the important meta data and then you need to use *args (arguments stored as a tuple) and **kwargs (arguments stored as a dictionary). For applying this to next function you just have to write @function_name_here. Decorators are of four types: 1. Function decorator: these are applied to regular functions (just like in our example). 2. Method decorator: these are used specifically methods defined in class (I used this in my code). 3. Class decorator: these are applied directly to the class as a whole. 4. Built-in-decorator: python also offers some built in decorators to use with the class (I used @staticmethod decorator, this removes the need of putting self in the code). some commonly used decorators are @staticmethod, @classmethod, @property, @user_defined_decoratort etc. There are a lot of decorators however I have used user defined decorators (to create a logger) and static method decorator (because I didn't wanted to use self here) by doing I also realised that you can use more than one decorator in a single function. And Here's my code and it's result.

Reply
1

More like this

Recommendations from Medial

Sidharth Bidikar

Keep Moving • 10m

Which function can be used to combine text from different cells into one cell without using the CONCATENATE function? A) TEXTJOIN B) CONCAT C) JOIN #excel #dataanalyst #data #dataanalysis #dataanalytics

Reply
1
6

Sanskar

Keen Learner and Exp... • 7d

Day 17 of learning python as a beginner. Topic: lambda functions + email filter Lambda functions are a single line anonymous function without using the usual def key word. All function: checks whether all the key words (spam_keywords) are present

See More
Reply
2

Sanskar

Keen Learner and Exp... • 13d

Day 12 of learning python as a beginner. Topic: object oriented programming (OOP). Yesterday I shared a journal taking app and many people suggested that it would be great if I used classes. Initially I was not aware of what classes actually are an

See More
Reply
2

Sanskar

Keen Learner and Exp... • 14d

Day 11 of learning python as a beginner. Topic: creating journal entry program using file I/O. I got a little sidleline from my original plan of editing my to-do list program and created an entirely new program this time for writing a daily journal

See More
Reply
1
10

Sanskar

Keen Learner and Exp... • 2d

Day 23 of learning python as a beginner. Topic: multi-threading. Multi-threading is a technique to run multiple task (such as importing or downloading) within a single program and at the same time. Thus running multiple things parallelly, in a nuts

See More
Reply
2
Image Description
Image Description

Arpit Shukla

Helping people achie... • 1m

Why are we still coding in programming languages when plain but exact English statements should be used to define what functions and methods should do. Just a vision: 1. Let everyone (not only developers) write modular functions with plain English d

See More
8 Replies
14
Image Description
Image Description

Dave walt

Hey I am on Medial • 1y

How can this solar cell industry boom even among the middle class family. What are it's pros and cons at the moment to be used by middle class

7 Replies
10
Image Description

Rahul Gupta

Hey I am on Medial • 12m

The legal obligation of an AI should be to maximize the value function of the person it represents, and the goal of an AI President should be to maximize the collective value function of everybody. ~ Pedro Domingos

1 Reply
1
5

Sanskar

Keen Learner and Exp... • 20d

Day 5 of learning python as a beginner. Topic: Functions On my previous day post many people shared their github where I was introduced to def functions and since then I started learning more about def functions. Thank you all those people who are

See More
Reply

Sidharth Bidikar

Keep Moving • 10m

What is the most efficient way to combine text from multiple cells into one cell without using the CONCATENATE function? A) Using the TEXTJOIN function B) Using the LEFT function C) Using the SUBSTITUTE function #excel #msexcel #dataanalyst #dataan

See More
Reply
1
5

Download the medial app to read full posts, comements and news.