简体   繁体   中英

passing of a dictionary as a parameter between functions in python?

I am trying to pass a dictionary to a function so that it is the function's first parameter and that type checking occurs to verify that a dictionary has indeed been submitted.

fridge = {"cheese":10, "milk":11, "feta":12, "cream":21, "onion":32, "pepper":14}

def fridge_validation(fridge):
    if not isinstance (fridge,dict) :
        raise TypeError: ("require a valid dictionary to be submitted!")

I thought the following would work....

def dummy (fridge):

return fridge

test_of_dummy=dummy ({"cheese", "milk", "eggs"})

print (test_of_dummy)

{'eggs', 'milk', 'cheese'} (this was what was printed)

Not quite sure if I have done it correctly? Also.... I am confused by the following.

 def dummy (fridge):
    fridge={}
    return fridge





  test_of_dummy=dummy ({"cheese", "milk", "eggs"})
    print (test_of_dummy) "{}" was outputted...

.

but I thought I had already passed on the variables...? So why is the {} seemingly taking precedence over the test_of_dummy ?

As to what I am trying to do....

1) Pass a dictionary called fridge as the 1st parameter to a function. Use isinstance and type error to confirm the dictionary is indeed a dictionary.

2) Have a second function that will subtract from the fridge dictionary

Note: {"cheese", "milk", "eggs"} is a set.

In your first function, you just return the argument, so it's no surprise what you got. In the second one, you set fridge to an empty set before returning it, so you get an empty set returned.

All this seems rather unnecessary though, what exactly are you trying to do, and what do you mean you can only manipulate the dictionary once?

In order to validate your fridge variable you can follow this example:

fridge = {"cheese":10, "milk":11, "feta":12, "cream":21, "onion":32, "pepper":14}

def fridge_validation (fridge = {}):
    # Test if fridge is a dict type
    if not isinstance(fridge, dict):
        # if fridge isn't a dict type, throw an exception
        raise TypeError("require a valid dictionary to be submitted!")
    # if fridge is a dict type return it. So you got your validation
    else:
        return fridge

validation = fridge_validation(fridge)
print(validation)

Output:

{'milk': 11, 'feta': 12, 'onion': 32, 'cheese': 10, 'pepper': 14, 'cream': 21}

However, {"cheese", "milk", "eggs"} is a set type and not a dict type .

You can verify it by using Python interpreter :

>> a = {"cheese", "milk", "eggs"}
>> type(a)
<class 'set'>

So as you see, {"cheese", "milk", "eggs"} is a set not a dict . So if you pass it into your fridge_validation() your code will throw an exception.

Also, in your method:

def dummy(fridge):
    fridge = {}
    return fridge

The return will be always equal to {} which is an empty dict type the cause is your code will always overwrite your fridge value by an empty dict.

An answer to your second question.

How can you subtract values from your dict ? The answer is simple:

I suppose you got a dict after validating it with your fridge_validation() method. For example:

validation = {"cheese":10, "milk":11, "feta":12, "cream":21, "onion":32, "pepper":14}

So:

print(validation["cheese"])

output:

10

also:

print(validation["milk"])

output:

11

And so on for the rest. In summary, in order to substract values from a dict you can use: dictionary["key"] this will output the key's value.

Also, i recommend you reading this tutorial in order to understand how you can deal with python dicts .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM