FAQ Database Discussion Community


How to use *args and **kwargs with __init__ in a smart and elegant way in Python?

python,kwargs
From docu and from some tutorials I know the basics about *args and **kwargs. But I think about how to use them with __init__ in a nice and pythonic way. I added this pseudo code to describe the needs. __init__() should behave like this: If the parameter name it should...

Django Forms: Empty kwargs from Views

python,django,forms,parameters,kwargs
I am trying to initialize a Django form with a parameter from a view. However when i try to use the kwargs in the form's init, i get that it's always empty. Why could it be? Am i missing something? My view (filename: “FirstView.py”): def create_userStory(request, proyect_id): if request.method=='POST': auxForm...

Django kwargs expansion not working with startswith

python,django,database,filter,kwargs
I was facing a problem very similar to question 310732 and following the advice there, I implemented the .format() solution in my kwargs generation. A dump of a small example looks like this: {'username_fragment_1__startswith': u'joey'} where "username_fragment_1" is a valid column name. More details per Daniel's request: query_sub = "(CASE...

Removing infinity values of a function using exception handling, *args, and **kwargs

python-2.7,exception-handling,args,kwargs,gradient-descent
I'm currently working through the book Data Science from Scratch by Joel Grus, and I've run across a function that I don't really understand: def safe(f): def safe_f(*args, **kwargs): try: return f(*args, **kwargs) except: return float('inf') return safe_f The safe function is called within a Gradient Descent algorithm to remove...

arbitrary number of *args or ** kwargs in str.format() in Python

python,python-3.x,string-formatting,args,kwargs
I'm making a socket client and need to define a function which sends the following kind of message to server: sometext,[name_1],[name_2],...,[name_n]. Actually the message is more complex, but for the sake of an example I simplified it. Where: ... - represents the rest of the names (comma delimited) between name_2...