python 3中的ctypes.cast()返回函数地址而不是函数返回值

时间:2019-08-11 11:43:00

标签: python-3.x

当我运行以下代码时:

def month_1():
    return 'January'

def month_2():
    return 'February'

def to_months(argument):
   switcher = {1:month_1,2:month_2}
   func = switcher.get(argument, 
          lambda: " Error found!")
   return func


address = id(to_months(1))

print(ctypes.cast(address, 
ctypes.py_object).value)

我明白了:

  

“ function month_1 at 0xf58abc48”

代替此:

  

“一月”

如何获取函数的返回值而不是函数的地址?

如果我做了这样的事情:

df = 5
fg = id(df)
print(ctypes.cast(fg, 
ctypes.py_object).value)

我会得到这个:

  

'5'

而不是值的地址

0 个答案:

没有答案