簡體   English   中英

如何獲得函數調用的別名?

[英]How do I get the alias of a function call?

我遇到的問題是我不知道如何獲取函數調用的別名。

代碼的作用是跟蹤文本:

"struct(...)"

在代碼中:

x = struct(...)

致電時:

x()

IDE的工作方式是“ x = struct(...)”,它僅在調用樹中定義了一個區域,該區域在樹中創建了一個縮進,實際上它並沒有對文件數據做任何事情。
當您調用“ x()”時,文件數據以“ color = hash(“ struct(...)”)”的顏色突出顯示。

因此,我需要獲取別名“ x”,以便可以在代碼中跟蹤呼叫...


我無法完全幫助任何人重現此代碼,因為這樣做的代碼相當大...
但我只需要一些想法,因為我似乎在Google上找不到任何不錯的例子。

我正在尋找以下情況:
顯而易見的:

x = struct(...)

不太明顯:

p,x,t = 0, struct(...), True

高度無關的:

p,x,t = (
    0,
    struct(...),
    True )

全部導致對x()的調用

我正在使用令牌化來獲取struct()的調用名稱,並且整個代碼都存儲在“ self.codeeditor.data”中...

如何使用“ struct”獲取“ x”?

編輯:我可以提到x將是由struct()返回的動態創建的_SUB_STRUCT類的實例。

我認為tokenize在這里不會真正起作用; 您最好使用ast在語法樹級別上進行工作並尋找Assign節點。

例如:

>>> [n.targets[0].id for n in ast.walk(ast.parse("x = struct()"))
    if isinstance(n, ast.Assign)
        and isinstance(n.value, ast.Call)
        and n.value.func.id == 'struct']
['x']

您應該能夠使用dict來獲得所需的功能:

# Relates a label to a function
function_maps = {'a':str, 'b':int, 'c':print}

# Will print 'Hello World!'
function_maps['c']('Hello World!')

# Lets look for the `int` function
for key, value in function_maps.items():
    if value == int:
        print('The alias for `int` is: {}'.format(key))

在沒有看到更多代碼的情況下,這是我建議這樣做的方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM