Minimal reproducible example:
list1 = [1,2,3,4,5,6,7,8,9,10]
class Myclass:
def __init__(self,list1):
self.list1 = list1
def pull(self, process_func):
list1 = self.list1
for n in list1:
process_func(n)
def print_func(n):
print(n)
sub = Myclass(list1=list1)
sub.pull(process_func=print_func)
# works fine print out all the elements in the list
I want to add more arguments to the print_fun to become something like this:
def print_func(n, k):
n = n k
print(n)
Does K have to be specified within Myclass.pull? As I can't do something like:
sub = Myclass(list1=list1)
sub.pull(process_func=print_func(k=1))
# TypeError: print_func() missing 1 required positional argument: 'n'
Reason to do this is because Myclass is a fully working stand-alone, at some point I just want to pass in a function there to capture some info which does not affect Myclass running at all. Any thoughts?
Edit:
In the actual problem I am facing n is a json object and I will need to use .decode() on n. Using adding default value unfortunately is not fesible in my case.
CodePudding user response:
if you want your solution to work this way, then you need to set the default value to the functional arguments, then you can pass the value to function during functional call and functional passing
def print_func(n=0, k=0):
n = n k
print(n)
CodePudding user response:
This code seems to work fine with just changing the function:
list1 = [1,2,3,4,5,6,7,8,9,10]
class Myclass:
def __init__(self,list1):
self.list1 = list1
def pull(self, process_func):
list1 = self.list1
k = 5
for n in (list1):
process_func(n, k)
def print_func(n, m=0): # Adding default if m is not specified
print(n m)
sub = Myclass(list1=list1)
sub.pull(process_func=print_func)
If you want to pass the arguments with sub.pull()
you can just create another argument for pull()
which specifies the arguments for process_func()
like this:
list1 = [1,2,3,4,5,6,7,8,9,10]
class Myclass:
def __init__(self,list1):
self.list1 = list1
def pull(self, process_func, k): # Added extra argument
list1 = self.list1
for n in (list1):
process_func(n, k)
def print_func(n, m=0): # Adding default if m is not specified
print(n m)
sub = Myclass(list1=list1)
sub.pull(process_func=print_func, k=5) # Passing k along with the sub.pull()
CodePudding user response:
I don't know exactly what you are trying to do, but you may be able to solve it with the code below.
list1 = [1,2,3,4,5,6,7,8,9,10]
class Myclass:
def __init__(self,list1):
self.list1 = list1
def pull(self, process_func, k=0):
list1 = self.list1
for n in list1:
process_func(n, k)
def print_func(n, k):
print(n k)
sub = Myclass(list1=list1)
sub.pull(process_func=print_func, k=1)
# works fine print out all the elements in the list
CodePudding user response:
As I can't do something like:
sub.pull(process_func=print_func(k=1))
Actually you can, using functools.partial
:
from functools import partial
sub.pull(process_func=partial(print_func, k=1))
Similarly, you can use lambda-functions:
sub.pull(process_func=lambda x: print_func(x, k=1))
Another approach is to use *args
and **kwargs
in the definition of Myclass.pull()
def pull(self, process_func, *args, **kwargs):
list1 = self.list1
for n in list1:
process_func(n, *args, **kwargs)
Then you can do something like this:
sub.pull(process_func=print_func, k=1)
or
sub.pull(process_func=print_func, 1)