Server Side Programming Articles

Page 5 of 2108

How can we define a Python function at runtime?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 12-Mar-2026 107 Views

We can define a python function and execute it at runtime by importing the types module and using its function types.FunctionType() as follows This code works at the python prompt as shown. First we import the types module. Then we run the command dynf=…; then we call the function dynf() to get the output as shown import types dynf = types.FunctionType(compile('print("Really Works")', 'dyn.py', 'exec'), {}) dynf() Output Really Works Defining a Python function at runtime can be useful in a variety of situations, such as when you need to ...

Read More

What are Python function attributes?

Tarun Chandra
Tarun Chandra
Updated on 12-Mar-2026 7 Views

Python is made up of objects but can also be used in other ways. Similarly, functions are also made up of objects, since they are objects they have attributes like identifier, title, inputs, outputs, etc. The value of an attribute cannot be changed once it has been set. To set the value of an attribute we use a function setattr and to know what value has been set as the attribute we use a function getattr. To access the attributes of a function we use the ‘.’ Operator. Some of the attributes have a special functionality such that they can ...

Read More

How can we create recursive functions in Python?

Sarika Singh
Sarika Singh
Updated on 12-Mar-2026 4K+ Views

Recursion is a process where a function calls itself repeatedly with new input values, slowly moving toward a condition where it stops. This stopping condition is called the base case. It prevents the function from running endlessly and allows it to return a final result. In Python, we can create recursive functions by simply defining a function that calls itself. Every recursive function has two main components − Recursive Case: This is when the function calls itself to solve a smaller part of the problem. It keeps the process going. Base Case: This is the stopping point. It tells ...

Read More

How to get a list of parameter names inside Python function?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 12-Mar-2026 3K+ Views

To get a list of parameter names inside a Python function, you can use the inspect module. This module provides several functions that let you examine the properties of Python objects, including function signatures, parameter names, and default values. Using inspect.getfullargspec() The inspect.getfullargspec() function returns a named tuple containing information about a function's parameters, including argument names, variable args, keyword args, and default values ? Example import inspect def aMethod(arg1, arg2): pass print(inspect.getfullargspec(aMethod)) def foo(a, b, c=4, *arglist, **keywords): pass print(inspect.getfullargspec(foo)) The output of the above ...

Read More

How to wrap python object in C/C++?

Gireesha Devara
Gireesha Devara
Updated on 12-Mar-2026 651 Views

To wrap existing C or C++ functionality in Python, there are number of options available, which are: Manual wrapping using PyMethodDef and Py_InitModule, SWIG, Pyrex, ctypes, SIP, Boost.Python, and pybind1. Using the SWIG Module Let’s take a C function and then tune it to python using SWIG. The SWIG stands for “Simple Wrapper Interface Generator”, and it is capable of wrapping C in a large variety of languages like python, PHP, TCL etc. Example Consider simple factorial function fact() in example.c file. /* File : example.c */ #include // calculate factorial int fact(int n) { ...

Read More

How to replace blanks in a vector with the previous element in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 243 Views

Filling of blanks is not an easy task in data analysis, especially if the vector contains numerical or integer values. Suppose we have a vector x that contains 1, , 2, 3, 4, 5 and we want to put 1 in place of blank after first value then cummax function along with seq_along function can be used as x[cummax(seq_along(x)*(x!=""))].Example1x1

Read More

How to create a vector with all dates in a particular year in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

We know that some years are leap years and some are normal years. The leap years have 366 days and the normal years have 365 days. To create a vector with all dates in a particular year we can use first date and the last date of the year by reading them with as.Date and creating a sequence with seq function. Check out the below examples to understand how it is done.Example1Creating a vector with dates in year 2020 −seq(as.Date("2020-01-01"), as.Date("2020-12-31"), by="1 day")Output[1] "2020−01−01" "2020−01−02" "2020−01−03" "2020−01−04" "2020−01−05" [6] "2020−01−06" "2020−01−07" "2020−01−08" "2020−01−09" "2020−01−10" [11] "2020−01−11" "2020−01−12" "2020−01−13" "2020−01−14" "2020−01−15" ...

Read More

How to concatenate two or more vectors in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 5K+ Views

The concatenation of vectors can be done by using combination function c. For example, if we have three vectors x, y, z then the concatenation of these vectors can be done as c(x,y,z). Also, we can concatenate different types of vectors at the same time using the same same function.Example1set.seed(999) x1

Read More

What is the difference between creating a matrix by using matrix function or as.matrix function in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 312 Views

The difference between as.matrix and matrix function is that nrow argument or ncol argument are not helpful with as.matrix function but with matrix function we can use them. Therefore, we can actual define a matrix with matrix function but if we have a data frame or data table then it can be converted to matrix by using as.matrix function.Examples of creating matrix with as.matrix and matrix functionExample1M

Read More

How to calculate mahalanobis distance in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 731 Views

The Mahalanobis distance is the relative distance between two cases and the centroid, where centroid can be thought of as an overall mean for multivariate data. We can say that the centroid is the multivariate equivalent of mean. If the mahalanobis distance is zero that means both the cases are very same and positive value of mahalanobis distance represents that the distance between the two variables is large. In R, we can use mahalanobis function to find the malanobis distance.Example1y1

Read More
Showing 41–50 of 21,074 articles
« Prev 1 3 4 5 6 7 2108 Next »
Advertisements