Can't I use the function as a dictionary value?

Asked 2 years ago, Updated 2 years ago, 72 views

For example, Assuming that the functions draw_nose and draw_eye are defined

drawing = { 'nose' : draw_nose, 'eye' : draw_eye}

Define a drawing dictionary and

If I enter the key value of the dictionary, can't the function of the value be executed?

python dictionary function

2022-09-21 10:10

1 Answers

why not

>>> def dog barking (): print ("Boo-boo-boo~~")

>>> def Cat barks (): print ("Meow Meow Meow meow~)~~")

>>> Animal = { "Dog": Dog barking, "Cat": Cat barking}
>>> Animal ["Dog"]
<function dog barking at 0x0000018B03474D90>
>>> Animal ["Dog"]()
Woof woof woof!
>>> Animal ["Cat"]()
Meow, meow~


2022-09-21 10:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.