Keen Learner and Exp... • 21d
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... • 1m
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 MoreKeen Learner and Exp... • 22d
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 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 MoreKeen Learner and Exp... • 15d
Day 30 of learning python as a beginner. Topic: making a fully functional calculator. Yesterday I got introduced to tkinter and was successful in creating a basic graphical layout for the calculator and today I added logic into it and now I finally
See MoreDownload the medial app to read full posts, comements and news.