Inside my class I have:
def refresh(self, func = None, *args):
if not func:
return
new_data = func(*args)
self.set_text(new_data)
But I have 2 problems:
-
What if
funcdoesn’t take any parameters? Why can’t I do*args = None?
like I want to do something like this:def refresh(self, func = None, *args = None): if not func: return if *args: new_data = func(*args) else: new_data = func() # func doesn't take any parameters self.set_text(new_data) -
When I try to run my code before the edits I get:
TypeError: function takes 0 positional arguments but 1 were given
Why is that?
Here is my main:
def hello_world():
return 'hello'
tmp2 = LLabel('Hi There!')
tmp2.refresh(hello_world, None)
>Solution :
None is an argument of NoneType. You can try it simply by running the hello_world function by itself.
def hello_world():
return 'hello'
hello_world(None)
Which will result in the following error: TypeError: hello_world() takes 0 positional arguments but 1 was given.
Hence run the refresh method, with only one argument – the function you want to pass in:
def hello_world():
return 'hello'
tmp2 = LLabel('Hi There!')
tmp2.refresh(hello_world)
Therefore the original refresh method can stay the same:
def refresh(self, func = None, *args):
if not func:
return
new_data = func(*args)
self.set_text(new_data)