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.
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 MoreKeen 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 MoreHelping 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 MoreDownload the medial app to read full posts, comements and news.