-1

I want my function can be called by this way,

And the order of parameters is insensitive.

The two functions identical

search(categories=["oll","Academic Data"], names=["mary","jack"])

search(names=["mary","jack"], categories=["oll","Academic Data"])

I can also pass that way

search(names=["mary","jack"])

Finally, I hope i can get the parameters into an dictionary.

I want the parameters to be explicit but rather than dictionary Thanks

Naman Sogani
  • 943
  • 1
  • 8
  • 28
newBike
  • 14,385
  • 29
  • 109
  • 192
  • SO you want to call a user-defined function `search` which takes a minimum one argument names and produces a dictionary as output – The6thSense Jul 08 '15 at 06:14

4 Answers4

2

Have you tried:

def search(names = [], categories = []):
     print(names)
     print(categories)

>>> search(names=["mary","jack"])
['mary', 'jack']
[]
>>> search(categories=["oll","Academic Data"], names=["mary","jack"])
['mary', 'jack']
['oll', 'Academic Data']

If you do not want the 'names' arg to be optional, just remove the default [] in function args.

Ananth
  • 4,227
  • 2
  • 20
  • 26
2

You can define the function as -

def foo(**kwargs):
    print(kwargs)

This will allow named parameters to be passed in, and kwargs would be a dictionary of those named parameters -

>>> foo(categories=["oll","Academic Data"], names=["mary","jack"])
{'categories': ['oll', 'Academic Data'], 'names': ['mary', 'jack']}
>>> foo(names=["mary","jack"], categories=["oll","Academic Data"])
{'categories': ['oll', 'Academic Data'], 'names': ['mary', 'jack']}
>>> foo(names=["mary","jack"])

Or another option (if you want to be explicit about the parameters) is to set a default value for the parameters, when using named arguments, they can be in any order, exmaple -

>>> def foo1(categories=None,names=None):
...     print('categories - ' + str(categories))
...     print('names - ' + str(names))
...
>>> foo1(categories=["oll","Academic Data"], names=["mary","jack"])
categories - ['oll', 'Academic Data']
names - ['mary', 'jack']
>>> foo1(names=["mary","jack"], categories=["oll","Academic Data"])
categories - ['oll', 'Academic Data']
names - ['mary', 'jack']
>>> foo1(names=["mary","jack"])
categories - []
names - ['mary', 'jack']

Even without default values, when you are using named arguments in function calling, the order of the arguments do not matter, they only matter when you do not specify the names of the arguments when calling the function). Example -

>>> def func(a, b):
...     print(a, b)
...
>>> func(b=1,a=2)
2 1
>>> func(a=2,b=1)
2 1

Adding default value for the argument, makes them optional during calling.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
1
def search(names, categories = None):
    if categories:
        return ({"name":names,"categories":categories})
    return ({"name":names})

Output:

search(names=["mary","jack"])
{'name': ['mary', 'jack']}

search(names=["mary","jack"],categories=["man"])
{'categories': ['man'], 'name': ['mary', 'jack']}

If you want to provide a optional variable provide it's type when defining i.e.)categories = [] if you want a permanent argument don't define it's type i.e.)names

The6thSense
  • 8,103
  • 8
  • 31
  • 65
0

Looking for this?

def search(**kwargs):
    print(kwargs)

search(categories=["oll","Academic Data"], names=["mary","jack"])
search(names=["mary","jack"], categories=["oll","Academic Data"])
search(names=["mary","jack"])

Prints:

{'names': ['mary', 'jack'], 'categories': ['oll', 'Academic Data']}
{'names': ['mary', 'jack'], 'categories': ['oll', 'Academic Data']}
{'names': ['mary', 'jack']}
Sait
  • 19,045
  • 18
  • 72
  • 99