we have a class that does a lot of very important stuff and we want to time everything it does. Then we should use something like a
time_this decorator on every method inside the class:
That would work just fine. But it’s quite a few extra lines of code within the class. And what if we write some more class methods and forget to decorate one of them? What if we decide we dont want to time the class any more? There is definitely space for…
What is a decorator ? Types of decorators ?
What is the order of execution of a decorator ?
Can we pass arguments to the decorators ?
Can there be multiple decorators ? What is the order of execution in this case ?
What are various built in decorators available in python?
What is the real time use cases when we need to use decorators ?
Python is a pretty special language in that functions are first class objects. What that means is once a function is defined in a scope it can be passed to functions, assigned…
Bounded Knapsack where each item is supplied in limited numbers. We can use the item only once.
Given two integer arrays to represent weights and profits of ’N’ items, we need to find a subset of these items which will give us maximum profit such that their cumulative weight is not more than a given number ‘C’. Each item can only be selected once, which means either we put an item in the knapsack or skip it.
Below is the template to write a DP code:
Step1: First define all the edge cases handling from where our code needs to…
In Python, the things that are occurring simultaneously are called by different names (thread, task, process) but at a high level, they all refer to a sequence of instructions that run in order.
only multiprocessing actually runs these trains of thought at literally the same time. “Threading and asyncio” both run on a single processor and therefore only run one at a time. They just cleverly find ways to take turns to speed up the overall process.
Even though they don’t run different trains of thought simultaneously, we still call this concurrency.
In this section we will cover only portions…
Protocol Buffers provide an efficient way to encode structured data for serialization. The language’s basic organizational type is a message, which can be thought of as a C-style structure. It is named and contains some number of fields.
With protocol buffers, you write a .proto description of the data structure you wish to store. From that, the protocol buffer compiler creates a class that implements automatic encoding and parsing of the protocol buffer data with an efficient binary format. …
For a programmer who works in c++ or java or in that sense works in any object oriented programming, polymorphism means same function behaving differently while accepting different types and different number parameters. It is called as Function overloading. Some time we need this polymorphic behavior , for example i was working on some assignment where i need to implement visitor design pattern in python …
Function overloading in python
Lets walk through an example:
# to calculate area of rectangle
def area(length, breadth):
calc = length * breadth