Jurudocs
Jurudocs

Reputation: 9175

Python Basics: How can i set two function parameters in equations?

I have a basic question on python functions and parameters

Given this function:

def findArticleWithAttr(tableattrib, user_url_input):
    articles = Something.objects.filter(tableattrib=user_url_input) 

I call the function with:

findArticleWithAttr(attribute1, userinput1)

tableattrib is not set by findArticleWithAttr attribute1 and just takes the latter (userinput1) parameter and replaces it.

How can i make python set both parameters in the equation?

Upvotes: 2

Views: 232

Answers (1)

unutbu
unutbu

Reputation: 880667

You could use the ** double-splat operator:

def findArticleWithAttr(tableattrib, user_url_input):
    articles = Something.objects.filter(**{tableattrib : user_url_input}) 

Basically, the ** operator makes

func(**{'foo' : 'bar'})

equivalent to

func(foo = 'bar')

It allows you to call a function with arbitrary keyword arguments.

Upvotes: 8

Related Questions