Created
January 8, 2015 13:39
-
-
Save evertheylen/8dc1a07f0d1600650554 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A basic function works like this: | |
def A_times_B_plus_C(a, b, c): | |
return (a*b) + c | |
# a, b and c are positional arguments. I define them by putting a value in the right place. | |
print(A_times_B_plus_C(5, 10, 6)) # == 56 | |
# however, as functions grow large, they might have a lot of parameters, and you don't want to | |
# remember every place. Python solves this with keyword arguments (short: kwargs). | |
print(A_times_B_plus_C(a=5, b=10, c=6)) | |
print(A_times_B_plus_C(c=6, a=5, b=10)) | |
# As you can see, the place doesn't matter anymore, and the parameters aren't defined by their | |
# place anymore, but by their name. | |
# Python implements this using dictionaries, so we can take advantage of that! | |
# let's redefine our function. | |
def A_times_B_plus_C(**kwargs): | |
return (kwargs['a'] * kwargs['b']) + kwargs['c'] | |
# Note the '**' before kwargs. That tells python that it should treat kwargs not as a regular | |
# dictionary, but as special keyword dictionary. As an example, we can also write it without | |
# the '**': | |
def no_stars_A_times_B_plus_C(kwargs): | |
return (kwargs['a'] * kwargs['b']) + kwargs['c'] | |
# However, the function above is less elegant to call: compare these two function calls: | |
print( A_times_B_plus_C( c = 6, a = 5, b = 10) ) | |
print( no_stars_A_times_B_plus_C({'c': 6, 'a': 5, 'b': 10}) ) | |
# they do exactly the same. Note that now we redefined our function, we can't call it | |
# with positional arguments anymore. | |
#print(A_times_B_plus_C(5, 10, 6)) # this will give an error, uncomment if you need proof :) | |
# In the kinepolis project, we mainly use **kwargs to 'pass on' some parameters. | |
# for example, when creating a datastructure: | |
# def createDataStructure(T, attr, **kwargs): | |
# <...> | |
# return T(attr, **kwargs) | |
# as you can see, createDataStructure() takes two positional arguments, and **kwargs. | |
# later in the function, we 'pass on' **kwargs to the initializer of our datastructure. | |
# for example, if we call createDataStructure("Hashmap", "ID", length=51) | |
# the 'length' parameter will end up in the dictionary kwargs, which we then 'pass on' to the | |
# initializer of the hashmap: | |
# return T(attr, **kwargs) | |
# In this line of code, T has been replaced by the type structures.Hashmap, so our Hashmap.__init__() | |
# will actually know what length we want the hashmap to be. | |
# class Hashmap: | |
# def __init__(self, attr, length=21, hashFuncString=None, collisionSolution = USLinkedChain, toInt = int): | |
# <...> | |
# another example to demonstrate the fact that kwargs really is just a dictionary: | |
def A_times_B_plus_C(a, b, c): | |
return (a*b) + c | |
def very_unhandy_function_call(function_to_call, I, dont, care, about, these, arguments, **keyword_arguments): | |
# Hack the kwargs (which can be called whatever you want btw) | |
keyword_arguments['a'] = 0 | |
print(function_to_call(**keyword_arguments)) | |
very_unhandy_function_call(A_times_B_plus_C, 1, 2, 3, 4, 5, 6, c=10, a=50, b=20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment