Skip to content

Commit dad423c

Browse files
committed
Python notes: Calling method or function by name
1 parent a5e9ad6 commit dad423c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Python.adoc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@ def write_temp_file(content):
100100
return path
101101
----
102102

103+
=== Calling method or function by name
104+
105+
* Calling method:
106+
+
107+
[source,python,linenums]
108+
----
109+
bar = getattr(foo, 'bar')
110+
result = method(arg_1, arg_2)
111+
----
112+
113+
* Calling function:
114+
+
115+
[source,python,linenums]
116+
----
117+
locals()["myfunction"]()
118+
# or
119+
globals()["myfunction"]()
120+
----
121+
+
122+
`locals` returns a dictionary containing local symbol table. `globals`
123+
returns a dictionary with global symbol table.
124+
103125
=== Trapping `Ctrl-C` but not SIGTERM
104126

105127
By using `KeyboardInterrupt` exception:

0 commit comments

Comments
 (0)