[Tutor] Why is it invalid syntax to have a particular dictionary value as an argument?

Dave Angel davea at davea.name
Mon Apr 6 19:54:58 CEST 2015


On 04/06/2015 12:43 PM, boB Stepp wrote:

>
> I was breaking down longer functions into smaller ones. Along the way
> I noticed I was passing an entire dictionary from one function to
> another. I only needed to pass one particular value, not the whole
> dictionary, so that is how I got into the issue I asked about.

Just to reinforce something you probably know well, passing a dictionary 
takes no more memory or time than passing an item from that dictionary. 
  The real choice is whether the called function should dealing with a 
single item or with a dictionary.  It would have a different name in 
each case, and a different set of reuse possibilities.

I know the following example abuses the dictionary, using it as though 
it were an instance of class Person.  It's just what popped into my head.

def check_person(person):
     if person["name"] in list_of_preferred:
          do_something...
          return True
     return False

def check_name(name):
     if name in list_of_preferred:
          do_something...
          return True
     return False

In the first case, the function would be able use other elements of the 
dictionary, like "email_address" or "phone_number".

In the second case, you could call the function from someplace that has 
only names, and isn't worried about what dict the name might be part of.


-- 
DaveA


More information about the Tutor mailing list