0

I am working with the Python Flickr API and have a problem defining a function (I am kind of new to Python).

I need to wrap the following api functionality into a function:

photos = flickr.photos_search(
    tags="foo",
    is_commons="True")

This returns a photo search for the tag "foo" from Flickr's Commons collection.

Now I want to replace the tag each time I search, so I wrap it in a function:

def search_photos(fl_tags):
    photos = flickr.photos_search(
        tags=fl_tags,
        is_commons="True")

search_photos("foo")

This works, the is_commons flag however, I need to occasionally replace altogether (not only the value, but also the key, since there seems to be a bug in Flickr's API that it always searches in the Commons collection no matter to which value you set the flag).

In this case I would like to replace it with a key-value combination license="9".

I don't know how to put that into the function parameters. If I provide a fl_license parameter and simply set that to license="9" when I call the function then this does not work (as I kind of expected).

def search_photos(fl_tags, fl_license):
    photos = flickr.photos_search(
        tags=fl_tags,
        fl_license)

search_photos("foo", license="9")    # --> SyntaxError: non-keyword arg after keyword arg

Is there any way to get this to work. How could I get the key-value pair through a function parameter?

Thanks!

mdomino
  • 1,195
  • 1
  • 8
  • 22
  • So basically pass `license="9"` into the `flickr.photos_search` function? Can you pass a `dict` there? – Zizouz212 Nov 15 '15 at 18:13
  • @Zizouz212 Yes, exactly. Passing a dictionary to flickr.photos.search did not work, but return a type error instead. photos = flickr.photos_search(fl_dict) TypeError: __call__() takes exactly 1 argument (2 given) – mdomino Nov 15 '15 at 18:47

2 Answers2

2

I believe you are looking for "Keyword Arguments". This StackOverflow post (Python normal arguments vs. keyword arguments) really helped me to understand the concept as I am also quite new.

Essentially any parameters received by the function from the call will be assigned into a dict with the name of the function argument designated with "**".

Community
  • 1
  • 1
  • Thanks, altough the questions is already answered, I will look into this, as I am not familiar with the several types of arguments. – mdomino Nov 15 '15 at 18:45
2

You could try something like this:

def search_photos(fl_tags, license_val=''):
    if not license_val:
        photos = flickr.photos_search(
            tags=fl_tags,
            is_commons="True")
    else:
        photos = flickr.photos_search(
            tags=fl_tags,
            license=license_val)
    return photos

search_photos("foo") # gives you the same output as before
search_photos("foo", "9") # gives you the search with license as 9

The variable = '' sets that parameter to an empty string by default if no argument is given (empty strings evaluate to false, so if you don't pass in a second arg, the function will default to a commons search). Otherwise, it will put whatever the second argument is as the license_val= parameter.

NotAnAmbiTurner
  • 2,553
  • 2
  • 21
  • 44