Skip to main content

All Questions

2 votes
1 answer
250 views

Write a retry mechanism in Python using functions as arguments

I'd like to write a retry mechanism in Python for unstable network communication. My service has multiple types of methods: method1() method2(arg1) method3(arg1, arg2) I'd like to pass the method ...
MedicineMan's user avatar
  • 15.3k
1 vote
2 answers
361 views

Python 2: how to pass arguments in tuple to a function? [duplicate]

I would like to pass a tuple of arguments args (at what the tuple varies in length due to some other functions) to a function, which works def myf(x1,x2,x3,....) return something involving x1,x2.....
Mauricio Fernández's user avatar
2 votes
1 answer
2k views

How to bind partial function as method on Python class instances

I want to bind this function as a method on some class: def foo_method(msg, self): print self.prefix + msg This works: class A(object): prefix = 'msg: ' A.foo = lambda *args: foo_method('...
nfelger's user avatar
  • 883
5 votes
2 answers
2k views

resolving map function issue in python 3 vs python 2 [duplicate]

I'm interested in functional programming with python and am working through Mary Rose Cook's blog post A practical introduction to functional programming. Apparently, it was written in python 2 as ...
hrokr's user avatar
  • 3,579
0 votes
2 answers
69 views

How to handle a huge collection of functions in Python 2.7

So I am working on this existing code base which has about 150 or so functions as time series I wanted to store them in a class in order to prevent namespace pollution. This is what exists import ...
Sahil Puri's user avatar
1 vote
3 answers
136 views

While recursing in functional programming (or otherwise), what are the benefits/limitations of working with an explicit state variable?

I was playing around with functional programming in Python, and realized there are two ways to replace loops with recursion. The canonical recursion technique does not seem to need any state, e.g., "...
vidit's user avatar
  • 957
4 votes
2 answers
745 views

Filtering function for an arbitrary nested list

I can't find out how to write this function as a lambda because of the double conditional: def f(e): if not isinstance(e,list): if e >10: return e else: return ...
user2314737's user avatar
  • 29.5k
0 votes
2 answers
2k views

Merge two lists python [duplicate]

a = [0, 2, 4] b = list(map(lambda x: x+1, a)) merged list c = [0, 1, 2, 3, 4, 5] c = [a[0], b[0], a[1], b[1] ... ] Can I achieve the result with functional programming ? Instead of just looping ? ...
Tony Tannous's user avatar
  • 14.9k
2 votes
1 answer
3k views

Python map on a group

There is an array of strings in this format: r = [["Name - Version - Author - Message"],[..],..] I run the following code on this data structure: from itertools import groupby m = map(lambda x: x....
stackErr's user avatar
  • 4,170
1 vote
2 answers
41 views

Possible to pass a function into a object's method as an argument, which can refer to that object?

def my_func(): stuff return something my_object.my_method(my_func) I want my_func to be able to refer to my_object. Essentially like deferred self, which means "whatever object I am in, refer ...
Jonny Shanahan's user avatar
1 vote
2 answers
67 views

How would someone know, when to pass an object as an argument to functions

Let say I have a string my_str = "Hello World" func1 = getattr(__builtins__, 'len') func1 <built-in function len> func1() Traceback (most recent call last):   File "<stdin>", line 1, in &...
Anurag Sharma's user avatar
5 votes
5 answers
21k views

Understanding nested lambda function behaviour in python

I'm trying to learn pure functional programming. But this code is puzzling me particularly the second line. I do not understand how the value 2 is passed to the variable x. Can somebody explain this ...
kmario23's user avatar
  • 61.7k
2 votes
1 answer
402 views

Chaining functions with multiple arguments

Is there a way to chain functions with multiple arguments? For the moment I "chain" my operation in Python like this: def createDirIfNecessary(directoryName): if not os.path.exists(directoryName)...
Colonel Beauvel's user avatar
-3 votes
3 answers
216 views

Expected output is not achieved here. How to fix that 9 is not a prime number? [duplicate]

Write a function that accepts a positive integer n as function parameter and returns True if n is a prime number, False otherwise. Note that zero and one are not prime numbers and two is the only ...
Karan Thakkar's user avatar
1 vote
6 answers
13k views

How to add odd numbers from a list in python?

Write a function that accepts a list of integers as parameter. Your function should return the sum of all the odd numbers in the list. If there are no odd numbers in the list, your function should ...
Karan Thakkar's user avatar

15 30 50 per page