你如何使用Fortran 90模块数据 [英] How do you USE Fortran 90 module data
问题描述
USE
语句中,您遵循以下约定: - 显式声明哪些变量/ functions /子程序只用于
:
语法,例如USE [module_name],仅限于:variable1,variable2,...
- 插入一个毯子
USE [module_name]
?
ol>
一方面, only
子句使得代码更加冗长。然而,它迫使你在代码中重复自己,如果你的模块包含很多变量/函数/子程序,事情开始显得不守规矩。
下面是一个例子:
模块常量
隐式无
实型,参数:: PI = 3.14
real,parameter :: E = 2.71828183
integer,parameter :: answer = 42
real,parameter :: earthRadiusMeters = 6.38e6
结束模块常量
程序测试
!选项#1:覆盖使用常量
!使用常量
!选项#2:指定您希望使用的每个变量。
使用常量,只有:PI,E,答案,earthRadiusMeters
隐式无
写(6,*)Hello world。下面是一些常量:
写(6,*)PI,&
E,&
答案,&
earthRadiusInMeters
结束程序测试
更新
希望有人说类似于Fortran的东西,只需在C#中重新编码它!,因此我可以投票给您。
更新
我喜欢 USE modulename 与Python的
-
'import module'or'from module import'
- 在回答中,Mark Roddy提到:
不要使用'from module import *'。对于
任何合理的大型代码集合,如果
'import *',您的可能将是
固定到模块中,无法
被移除。这是因为在代码中使用
的项目来自'module',
使它向东到达
这个点你认为你不知道的是
很难确定不再使用
导入,但它非常难以确定。
- 在回答中,Mark Roddy提到:
- dbr's answer 包含
不要做从x导入* - 它使
您的代码很难理解,因为
您不能轻易地看到方法
来自哪里(从x导入*;从y
导入*; my_func() - 其中是my_func
定义的?)
ul>
所以,我是le通过
USE模块名明确说明我在模块中使用的所有项目的共识,只有:var1,var2 ,...
[if]你有一个模块非常大,以至于你
感觉不得不添加,这意味着
你的模块太大了。拆分它。
这是一个平衡的问题。
如果您仅使用模块中的一些内容,则只需添加ONLY即可明确指定您正在使用的内容。
如果你在模块中使用了很多东西,只会指定很多东西,所以它的意义不大。你基本上是在挑选你使用的东西,但事实是你依赖于这个模块作为一个整体。
然而,最终的最好哲学是这一点:如果你担心命名空间污染,并且你有一个如此庞大的模块以至于你不得不加入,这意味着你的模块太大了。拆分它。更新:Fortran?只需在python中重新编码它;)
Let's say you have a Fortran 90 module containing lots of variables, functions and subroutines. In your USE
statement, which convention do you follow:
- explicitly declare which variables/functions/subroutines you're using with the
, only :
syntax, such asUSE [module_name], only : variable1, variable2, ...
? - Insert a blanket
USE [module_name]
?
On the one hand, the only
clause makes the code a bit more verbose. However, it forces you to repeat yourself in the code and if your module contains lots of variables/functions/subroutines, things begin to look unruly.
Here's an example:
module constants
implicit none
real, parameter :: PI=3.14
real, parameter :: E=2.71828183
integer, parameter :: answer=42
real, parameter :: earthRadiusMeters=6.38e6
end module constants
program test
! Option #1: blanket "use constants"
! use constants
! Option #2: Specify EACH variable you wish to use.
use constants, only : PI,E,answer,earthRadiusMeters
implicit none
write(6,*) "Hello world. Here are some constants:"
write(6,*) PI, &
E, &
answer, &
earthRadiusInMeters
end program test
Update Hopefully someone says something like "Fortran? Just recode it in C#!" so I can down vote you.
Update
I like Tim Whitcomb's answer, which compares Fortran's USE modulename
with Python's from modulename import *
. A topic which has been on Stack Overflow before:
‘import module’ or ‘from module import’
- In an answer, Mark Roddy mentioned:
don't use 'from module import *'. For any reasonable large set of code, if you 'import *' your will likely be cementing it into the module, unable to be removed. This is because it is difficult to determine what items used in the code are coming from 'module', making it east to get to the point where you think you don't use the import anymore but its extremely difficult to be sure.
- In an answer, Mark Roddy mentioned:
What are good rules of thumb for python imports?
- dbr's answer contains
don't do from x import * - it makes your code very hard to understand, as you cannot easily see where a method came from (from x import *; from y import *; my_func() - where is my_func defined?)
- dbr's answer contains
So, I'm leaning towards a consensus of explicitly stating all the items I'm using in a module via
USE modulename, only : var1, var2, ...
And as Stefano Borini mentions,
[if] you have a module so large that you feel compelled to add ONLY, it means that your module is too big. Split it.
It's a matter of balance.
If you use only a few stuff from the module, it makes sense if you add ONLY, to clearly specify what you are using.
If you use a lot of stuff from the module, specifying ONLY will be followed by a lot of stuff, so it makes less sense. You are basically cherry-picking what you use, but the true fact is that you are dependent on that module as a whole.
However, in the end the best philosophy is this one: if you are concerned about namespace pollution, and you have a module so large that you feel compelled to add ONLY, it means that your module is too big. Split it.
Update: Fortran? just recode it in python ;)
这篇关于你如何使用Fortran 90模块数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!