回答編集履歴

2

Update

2021/11/14 12:31

投稿

melian
melian

スコア20655

test CHANGED
@@ -16,9 +16,21 @@
16
16
 
17
17
 
18
18
 
19
- callers_name = inspect.stack()[-1].code_context[0].strip()
19
+ my_name = inspect.currentframe().f_code.co_name
20
20
 
21
+ callers_name = inspect.stack()[-1].code_context[0]
22
+
21
- callers_name = re.sub(r'^.*?(?<=()\s*(.+?)\s*,.*', r'\1', callers_name)
23
+ callers_name = re.findall(rf'\s*{my_name}\s*(\s*(.+?)\s*,', callers_name)
24
+
25
+ if callers_name:
26
+
27
+ callers_name = callers_name[0]
28
+
29
+ else:
30
+
31
+ return
32
+
33
+
22
34
 
23
35
  if callers_name in globals():
24
36
 
@@ -28,19 +40,19 @@
28
40
 
29
41
  if __name__ == '__main__':
30
42
 
31
- global_variable = 3
43
+ global_variable = 3
32
44
 
33
- print(f'before = {global_variable}')
45
+ print(f'before = {global_variable}')
34
46
 
35
47
 
36
48
 
37
- arbitrary_add(global_variable, 1)
49
+ arbitrary_add(global_variable, 1)
38
50
 
39
- print(f' after = {global_variable}')
51
+ print(f' after = {global_variable}')
40
52
 
41
-
42
53
 
54
+
43
- _ = '''
55
+ _ = '''
44
56
 
45
57
  before = 3
46
58
 

1

Update

2021/11/14 12:31

投稿

melian
melian

スコア20655

test CHANGED
@@ -15,10 +15,6 @@
15
15
  import inspect
16
16
 
17
17
 
18
-
19
- f = inspect.currentframe()
20
-
21
- v = inspect.getargvalues(f)
22
18
 
23
19
  callers_name = inspect.stack()[-1].code_context[0].strip()
24
20