Python - declare method name in dictionary with method definition in outside class

spottedzebra
from foo import fooClass

dict = {'a': method1, 'b': method2}

bar = fooClass()

method = dict['a']

bar.method()

I want to define a dictionary with a reference to a method but the method definition is not in the scope of the dictionary definition.

Currently, I get a NameError: name 'method1' is not defined.

To clarify, I have seen examples where functions are defined, then a dictionary using the function names is created in the same scope but this is not what I want to do.

user764357

You need to point the dictionary to the actual method:

from foo import fooClass

dict = {'a': fooClass.method1, 'b': fooClass.method2}

Since method1 isn't defined in that scope, you need to reference the method on the fooClass class.


Alternatively, if you didn't want to have to keep referening fooClass in your code, you could store the methods as strings and use getattr() do to this:

from foo import fooClass

dict = {'a': 'method1', 'b': 'method2'}

bar = fooClass()
method = getattr(bar.__class__, method = dict['a'])
bar.method()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python - declare method name in dictionary with method definition in outside class

From Dev

Declare method outside of class

From Dev

Declare a class property outside of a class method

From Dev

inner class method definition outside of a template class

From Dev

TypeScript - How to add a method outside the class definition

From Dev

Reference instance method outside class definition

From Dev

TypeScript - How to add a method outside the class definition

From Dev

Patch a method outside python class

From Dev

Patch a method outside python class

From Dev

Explicit use of `self` in method name in class definition

From Dev

Move method definition for template nested class outside declaration

From Dev

Wrapping method name in definition

From Dev

Declare String in class or method

From Dev

Python: How to invoke a method from outside a class?

From Dev

python call method from outside the class

From Dev

python method taking information outside class

From Dev

Dynamic method definition in a class

From Dev

regarding a method definition in a class

From Dev

Mocking a method outside of a class

From Dev

Declare and initialize in a method that has the same name as the class and data type

From Dev

How to access a dictionary outside of a method

From Dev

How do I define a Python method that I only want to use in the __init__ method, i.e., that is not accessible from outside the class definition?

From Dev

Why we declare static variable in a class & the definition in outside of the class?

From Dev

Order of method definitions and dictionary of class method references in Python

From Dev

Declare method inside class dynamically

From Dev

python: differences between init method and in-class declare?

From Dev

python: differences between init method and in-class declare?

From Dev

Ruby: class definition in method body

From Dev

Overriding a class's method definition

Related Related

  1. 1

    Python - declare method name in dictionary with method definition in outside class

  2. 2

    Declare method outside of class

  3. 3

    Declare a class property outside of a class method

  4. 4

    inner class method definition outside of a template class

  5. 5

    TypeScript - How to add a method outside the class definition

  6. 6

    Reference instance method outside class definition

  7. 7

    TypeScript - How to add a method outside the class definition

  8. 8

    Patch a method outside python class

  9. 9

    Patch a method outside python class

  10. 10

    Explicit use of `self` in method name in class definition

  11. 11

    Move method definition for template nested class outside declaration

  12. 12

    Wrapping method name in definition

  13. 13

    Declare String in class or method

  14. 14

    Python: How to invoke a method from outside a class?

  15. 15

    python call method from outside the class

  16. 16

    python method taking information outside class

  17. 17

    Dynamic method definition in a class

  18. 18

    regarding a method definition in a class

  19. 19

    Mocking a method outside of a class

  20. 20

    Declare and initialize in a method that has the same name as the class and data type

  21. 21

    How to access a dictionary outside of a method

  22. 22

    How do I define a Python method that I only want to use in the __init__ method, i.e., that is not accessible from outside the class definition?

  23. 23

    Why we declare static variable in a class & the definition in outside of the class?

  24. 24

    Order of method definitions and dictionary of class method references in Python

  25. 25

    Declare method inside class dynamically

  26. 26

    python: differences between init method and in-class declare?

  27. 27

    python: differences between init method and in-class declare?

  28. 28

    Ruby: class definition in method body

  29. 29

    Overriding a class's method definition

HotTag

Archive