Programming Articles - Page 3194 of 3363

What is @ operator in Python?

Jayashree
Updated on 30-Jul-2019 22:30:22

816 Views

@ symbol is used to define decorator in Python. Decorators provide a simple syntax for calling higher-order functions. By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.we have two different kinds of decorators in Python:Function decoratorsClass decorators A decorator in Python is any callable Python object that is used to modify a function or a class. A reference to a function  or a class  is passed to a decorator and the decorator returns a modified function or class. The modified functions or classes usually contain calls to ... Read More

How to implement Python __lt__ __gt__ custom (overloaded) operators?

Pythonista
Updated on 02-Mar-2020 09:52:46

4K+ Views

Python has magic methods to define overloaded behaviour of operators. The comparison operators (=, == and !=) can be overloaded by providing definition to __lt__, __le__, __gt__, __ge__, __eq__ and __ne__ magic methods.  Following program overloads < and > operators to compare objects of distance class. class distance:   def __init__(self, x=5,y=5):     self.ft=x     self.inch=y   def __eq__(self, other):     if self.ft==other.ft and self.inch==other.inch:       return "both objects are equal"     else:       return "both objects are not equal"   def __lt__(self, other):     in1=self.ft*12+self.inch     in2=other.ft*12+other.inch     if in1

Is there a “not equal” operator in Python?

Pythonista
Updated on 30-Jul-2019 22:30:22

442 Views

In Python 2.x as well as != symbols are defined as 'not equal to' operators. In Python 3, operator is deprecated.

How to save a Python Dictionary to CSV file?

SaiKrishna Tavva
Updated on 23-Sep-2024 14:29:19

44K+ Views

In Python to save a dictionary to a CSV file, we can use the CSV' module. This process slightly depends on the structure of your dictionary. Generally, a CSV file refers to each line is corresponds to a row in a table, and each value in the line is separated by a comma. CSV files are widely used because they are easy to read and write (handling files) and also easy to transfer data in the form of strings. Common Approaches There are various scenarios for saving a Python Dictionary to a CSV file, in this article, we focus ... Read More

How to split Python tuples into sub-tuples?

Vikram Chiluka
Updated on 09-Nov-2022 07:48:02

11K+ Views

In this article, we will show you how to split python tuples into sub-tuples. Below are the various methods to accomplish this task − Using slicing Using enumerate() & mod operator Tuples are an immutable, unordered data type used to store collections in Python. Lists and tuples are similar in many ways, but a list has a variable length and is mutable in comparison to a tuple which has a fixed length and is immutable. Using slicing Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task − Create a variable to ... Read More

What is "Argument-Dependent Lookup" ("Koenig Lookup") in C++?

Chandu yadav
Updated on 12-Feb-2020 06:34:31

380 Views

Argument-dependent lookup(ADL) is a protocol for looking up unqualified function names in function-call expressions.These function call expressions include implicit function calls to overloaded operators.The function names are looked up in the namespaces of their arguments in addition to the scopes and namespaces considered by the usual unqualified name lookup. Argument-dependent lookup makes it possible to use operators defined in a different namespace. Examplenamespace MyNamespace{    class A {};    void f( A &a, int i) {} } int main() {    MyNamespace::A a;    f( a, 0 );    //calls MyNamespace::f }The lookup of a function call to f was dependent ... Read More

What's the difference between "STL" and "C++ Standard Library"?

Govinda Sai
Updated on 24-Jun-2020 06:31:32

2K+ Views

The Standard Template Library (STL) is a software library for the C++ programming language that influenced many parts of the C++ Standard Library. It provides four components called algorithms, containers, functions, and iterators. Note that the term "STL" or "Standard Template Library" does not show up anywhere in the ISO 14882 C++ standard. So referring to the C++ standard library as STL is wrong, ie, STL and C++ Standard Library are 2 different things with the former being the subset of the latter.The STL consists ofContainersThe STL contains sequence containers and associative containers. Containers are objects that store data. The ... Read More

Why should C++ programmers minimize use of 'new'?

Ankith Reddy
Updated on 02-Mar-2020 08:07:45

152 Views

new is used for dynamic memory allocation. The memory allocated in this case goes on the heap. There are several costs associated with this type of memory allocation along with the programmer having to do manual memory cleaning and management. This type of allocation must be used when − You don't know how much memory you need at compile time.You want to allocate memory which will persist after leaving the current block.Other than these, there are very few cases where dynamic memory allocation is required. This is because, in C++, there is the concept of a destructor. This function gets called ... Read More

What is a "translation unit" in C++

Srinivas Gorla
Updated on 30-Jul-2019 22:30:22

3K+ Views

A translation unit is any preprocessed source file.A translation unit is the basic unit of compilation in C++. This unit is made up of the contents of a single source file after it passes through preprocessing. It contains included any header files without blocks that are ignored using conditional preprocessing statements like ifdef, ifndef, etc.A single translation unit can be compiled into an object file, library, or executable program.

Read whole ASCII file into C++ std::string

George John
Updated on 03-Dec-2024 09:40:15

15K+ Views

This is a simple way to read whole ASCII file into std::string in C++ − Algorithm Here is the algorithm we will follow to Read the whole ASCII file into C++ std::string. Begin  Declare a file a.txt using file object f of ifstream type to perform a read operation.  Declare a variable str of string type.  If(f) Declare another variable ss of ostringstream type. Call rdbuf() function to read the data of the file object. Put the ... Read More

Advertisements