![](/img/trans.png)
[英]Any way to get name of current lambda function within boto3 without specifying the name?
[英]There any way to get name of Fortran variable?
我喜歡它在 Python 中的實現方式。 示例(Python):
x = 1
y = 2
print(f"{x = }, {y = }")
# x = 1, y = 2
我想處理錯誤,然后打印變量名。 示例(Fortran):
function check(var)
...
if (var < 0) print *, 'Error: var < 0'
...
end function check
Fortran 無法動態獲取變量名,因為在編譯時需要變量名和類型。
您可以使用 Fortran 派生類型將 label 關聯到一個值:
program derived_types
! Define
type :: labelled_int_var
character(len=15) :: label
integer :: value
end type
! Declare
type(labelled_int_var) :: x,y
! Initialize
x%value = 1
x%label = 'x'
y%value = 2
y%label = 'y'
! Use
write(*,*) trim(x%label), " =", x%value, ", ", trim(y%label), " =", y%value
end program
對於 Intel 和 GNU fortran 編譯器(按問題標簽)以及許多其他編譯器,您可以使用預處理器( -cpp
標志)。
#if defined(__GFORTRAN__) || defined(NAGFOR)
# define CPPSTR(a) "a"
#else
# define CPPSTR(a) #a
#endif
#define assert(cond) if(.not. (cond)) print *, "Assert fail: ", CPPSTR(cond)
#define printv1(v1) print *, CPPSTR(v1), (v1)
#define printv2(v1,v2) print *, CPPSTR(v1), (v1), CPPSTR(v2), (v2)
#define printv3(v1,v2,v3) print *, CPPSTR(v1), (v1), CPPSTR(v2), (v2), CPPSTR(v3), (v3)
#define printarray(a) print *, CPPSTR(a); print *, (a)
program t
implicit none
integer i, j
i = 1
j = 3
assert(i .lt. 0)
assert(i .gt. 0)
printv3(i, j, i+j)
printv1(([i, j, i+j]))
end program t
Output:
Assert fail: i .lt. 0
i 1 j 3 i+j 4
([i, j, i+j]) 1 3 4
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.