How can I call a function with a dictionary?

python store function call in dictionary
python dictionary of functions with parameters
python call function with dictionary arguments
python define function in dictionary
call function inside dictionary python
python function input dictionary
python call function based on input
functions in a list python

I try to create a Dictionary to call some functions.

My array takes a key Int, a String and a function, like this:

let list_weapons: [Int: [Any]]  = [1: ["Sword", attack_sword],
                                   2: ["Magic wand", heal_magic_wand],
                                   3: ["Hammer", attack_hammer],
                                   4: ["Axe", attack_axe]]

These functions take a Class as a parameter, like this:

func attack_sword(character: Character)

I try to call my function like that but it doesn't work.

list_weapons[1]![1](character: Character) 

Cannot call value of non-function type 'Any'

If you have some ideas or advise me with another container

Thank you

Using an array of Any to store a string and a closure (function) with a specific signature is a poor choice. It would be better to declare a struct with two properties of the correct type. Then store those structures in your dictionary.

struct Action {
    let name: String
    let function: (Character) -> ()

let list_weapons: [Int: Action] = [
    1: Action(name: "Sword", function: attack_sword),
    // and the rest


Why store a function inside a python dictionary?, The technique is when you have a python dictionary and a function that you intend to use on it. You insert an extra element into the dict, whose value is the name of the function. When you're ready to call the function you issue the call indirectly by referring to the dict element, not the function by name. Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples. Accessing Values in Dictionary. To access dictionary elements, you can use the familiar square brackets along with the key to obtain its value.

You don't need to store methods in a dictionary to solve your problem. In my opinion, an architecture solution would be better here. You can play with selectors if you like, but I guess things will be much easier if you just do something like that

// Here you can specify all common thing about your weapons
protocol Weapon: class {
    var name: String { get }
    func attack(character: Character)

// Each weapon has its own class which can contain eveything you need
class Sword: Weapon { }
class MagicWand: Weapon { }
class Hammer: Weapon { }
class Axe: Weapon { }

// That's how you can store you weapons list
// You can use a dictionary if you like
let weaponsArray: [Weapon]  = [Sword(),

// And that's how you can use them
weaponsArray[0].attack(character: character)

1.12. Dictionaries, Construct a dictionary with String (or other) keys and unbound import string def function1(): print "called function 1" def function2(): print  Python has some methods that dictionary objects can call. For example, dict1.clear() method removes all items from the dictionary dict1. This page includes all dictionary methods available in Python 3. Also, the page includes built-in functions that can take dictionary as a parameter and perform some task.

You have to cast Any to the Function before using it.

let f = (list_weapons[1]![1] as! (Character) -> Void)
f("a") //it will invoke attack_sword("a")

And without temp variable,

(list_weapons[1]![1] as! (Character) -> Void)("a")

Using dictionaries to store data as key-value pairs, To do this, a method must be called on the construct. Now, let's look at the available methods for dictionaries. Let's use dict4 for this example. >>> dict4. >  7. In-Built Functions on a Python Dictionary. A function is a procedure that can be applied on a construct to get a value. Furthermore, it doesn’t modify the construct. Python gives us a few functions that we can apply on a Python dictionary. Take a look. a. len() The len() function returns the length of the dictionary in Python.

You can cast the element to a function type:

if let attack = list_weapons[1]![1] as? ((Character) -> Void) {
} else {
    print("Cannot attack")

But you better rework your data model. Using dictionaries can get confusing very quickly. Here's one way to do it:

protocol Weapon {
    var name: String { get }
    func attack(character: Character)

struct Sword: Weapon {
    let name = "Sword"
    func attack(character: Character) { ... }

struct MagicWand: Weapon {
    let name = "Magic Wand"
    func attack(character: Character) { ... }

struct Hammer: Weapon {
    let name = "Hammer"
    func attack(character: Character) { ... }

struct Axe: Weapon {
    let name = "Axe"
    func attack(character: Character) { ... }

let weapons: [Weapon] = [Sword(), MagicWand(), Hammer(), Axe()]
weapons[1].attack(character: ...)

You extend each weapon with different damages, unique features, levels, etc.

Dictionary of Methods/Functions « Python recipes « ActiveState Code, of listing all the functions in a Python dictionary and calling a relevant function user_input = input('- ') call, I try to use inputList.get(user_input) method to call  The pop removes the key/value pair from the dict but it's held as the return value so the assignment can happen. In the same fashion why this works: In the same fashion why this works: # exchange values a , b = b , a

Python Dictionary with Methods, Functions and Dictionary , AFAIK, everything is stored in dictionaries apparently. If you have a >> function called 'dude()' you could probably call it as a dictionary of >  Use parentheses when calling function procedures To use the return value of a function, assign the function to a variable and enclose the arguments in parentheses, as shown in the following example. If you are not interested in the return value of a function, you can call a function the same way you call a Sub procedure.

Using Python dict to call functions based on user input?, The popitem() method can be used to remove and return an arbitrary (key, value) item pair from the dictionary. All the items can be removed at once, using the  You can call the function directly, or by referencing the dict element whose value is the function. >>> find_city (cities, 'New York') NY >>> cities['_found'](cities, 'New York') NY Can someone explain what language feature this is, and maybe where it comes to play in "real" programming?

[Tutor] using a function as a dictionary value?, A dictionary in Python is a collection of data which is unordered and mutable. Unlike It is used for passing advanced data objects like dictionaries to a function because in such functions one Decorator to print Function call details in Python. A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. As you already know, Python gives you many built-in functions like print (), etc. but you can also create your own functions.

  • "You can't store methods in the array." - sure you can.