@cewing Let's say I have a dictionary that represents a unique customer. Something like:
customer = {
'first_name': first_name,
'last_name': last_name,
'phone_number': phone_number,
# ...
}
and that I want to write a function that only needs access to one or two of the values in the dictionary. What should I consider when trying to decide whether the function should accept the entire dictionary or just the values it needs access to? So, this:
def foo(customer_dict):
first_name = customer_dict['first_name']
# do something with first_name
foo(customer)
or:
def bar(customer_first_name):
#do something with customer_first_name
bar(customer['first_name'])
bar
above is going to refer to the 'name' that comes in, and that would be much clearer to read than something like "customer['name']" within the context of that function
@cewing Thanks. I'll keep your recommendation in mind. I can see an argument for both: Passing exactly what the function needs increases reusability, but passing an entire collection of things streamlines function call. I realized i was using both styles in one project, so I wanted to know what reasons you might have for picking one over the other. My trend seems to be to pass just what the function needs for functions that transform a single value such as:
sanitize_id(id_):
u"""Remove invalid username characters.
Characters are removed here instead of being labled
as invalid at username generation because the API
for one service automatically adds a special character
to the id that another service does not allow.
"""
but I also tend to pass entire collections to helper functions if the calling function accepts a collection: get_or_create_customer()
needs to accept a collection of customer attributes in case it needs to create a customer, so _get_customer()
accepts the same type of collection.
print arg