keyword-argument

Is it possible to pass kwargs to customised python enum

Is it possible to pass kwargs to customised python enum Question: You can customise an enum so the members have extra attributes. There are plenty of examples of that to be found. They all look something like this: class EnumWithAttrs(Enum): def __new__(cls, *args, **kwargs): value = len(cls.__members__) + 1 obj = object.__new__(cls) obj._value_ = value …

Total answers: 2

Problem with printing and sleeping in for loop with end key word argument

Problem with printing and sleeping in for loop with end key word argument Question: When I write the following code into python file/console: import time, os string = "what am I saying right now?" for x in string: print(x) time.sleep(.1) Output: w h a t a m I s a y i n g r …

Total answers: 1

How to call function with dict, while ignoring unexpected keyword arguments?

How to call function with dict, while ignoring unexpected keyword arguments? Question: Something of the following sort. Imagine this case: def some_function(a, b): return a + b some_magical_workaround({"a": 1, "b": 2, "c": 3}) # returns 3 I can’t modify some_function to add a **kwargs parameter. How could I create a wrapper function some_magical_workaround which calls …

Total answers: 4

kwargs different behavior

kwargs different behavior Question: Dear pythonist that question is for you! I don’t ask to solve my task, just ask for explaining why it happens) I know what is args and kwargs when they using but has been really shoked, when have found one thing. So, please check my example, here we pass arguments to …

Total answers: 2

Forcing python function parameters to not have order

Forcing python function parameters to not have order Question: I have a python function that takes a large amount of parameters : def func(p1=0, p2=0, p3=0, p4=0, p5=0, …, pN=0) -> None: pass I wanted to force the user to set the parameters as keyword arguments. I thought about one solution that seems off to …

Total answers: 1

Why mutable default parameter behaves this way?

Why mutable default parameter behaves this way? Question: I’m aware of mutable parameter behavior. Why the list is not set up to None when I send and unpack **dictionary as argument? The common_header.png repeads, it means is stored in list. That is weird to me and I couldn’t find answer for the question… I’m learning, …

Total answers: 2

How to change the number of arguments in a function call dynamically in Python?

How to change the number of arguments in a function call dynamically in Python? Question: I wrote the following Python code. def myFun(**kwargs): for key, value in kwargs.items(): print("%s = %s" % (key, value)) # Driver code a = input("Enter first word") b = input("Enter second word") c = input("Enter third word") d = input("Enter …

Total answers: 1

kwarg unpacking with mypy

kwarg unpacking with mypy Question: I have a function, that accepts inputs of different types, and they all have default values. For example, def my_func(a: str = ‘Hello’, b: bool = False, c: int = 1): return a, b, c What I want to do is define the non-default kwargs in a dictionary, and then …

Total answers: 1

Count frequency of item in tuple with kwargs**

Count frequency of item in tuple with kwargs** Question: This is what I wrote: def count_passes(**kwargs) count = 0 #Complete this function to count the number of passes for pas in kwargs: if pas == mark: count = count + 1 return result = count_passes(math="Fail", science="Fail", history="Pass", english="Pass") print(f’The number of passes: {count_occurrence(result, "Pass")}’) How …

Total answers: 3

Passing a function that takes **kwargs into concurrent.futures.ThreadPoolExecutor.map

Passing a function that takes **kwargs into concurrent.futures.ThreadPoolExecutor.map Question: I’ve just begun learning concurrent programming in Python. I created a function which takes one positional argument and then **kwargs. Code: def download_from_api(api_client_method, **kwargs): try: response = api_client_method(**kwargs) return response except api.exceptions.APIException as e: print(f'{kwargs}: {api_client_method.__name__} – {e}) I’d like to pass this function to concurrent.futures.ThreadPoolExecutor().map() …

Total answers: 1