你如何使用 Fortran 90 模块数据 [英] How do you USE Fortran 90 module data

查看:25
本文介绍了你如何使用 Fortran 90 模块数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个 Fortran 90 模块,其中包含 很多 的变量、函数和子例程.在您的 USE 语句中,您遵循哪种约定:

  1. 使用 , only : 语法明确声明您使用的变量/函数/子例程,例如 USE [module_name], only : variable1, variable2, ...?
  2. 插入毯子 USE [module_name]?

一方面,only 子句使代码更加冗长.然而,它迫使您在代码中重复自己,如果您的模块包含很多变量/函数/子例程,事情开始看起来不守规矩.

这是一个例子:

模块常量隐式无实数,参数 :: PI=3.14实数,参数 :: E=2.71828183整数,参数 :: answer=42实数,参数 :: earthRadiusMeters=6.38e6结束模块常量程序测试!选项#1:一揽子使用常量"!使用常量!选项#2:指定您希望使用的每个变量.仅使用常量:PI,E,answer,earthRadiusMeters隐式无write(6,*) "Hello world. 这里有一些常量:"写(6,*)PI,&E, &回答,&地球半径在米结束程序测试

更新希望有人说Fortran?用C#重新编码吧!"这样我就可以否决你.

<小时>

更新

我喜欢蒂姆·惠特科姆的回答,它将 Fortran 的 USE modulename 与 Python 的 from modulename import * 进行比较.之前在 Stack Overflow 上的一个话题:

  • 导入模块"或从模块导入"

    • 在回答中,Mark Roddy 提到:<块引用>

      不要使用从模块导入*".为了任何合理的大型代码集,如果你进口*"你可能会将其粘合到模块中,无法即将被删除.这是因为它是难以确定使用了哪些物品在代码中来自模块",向东进入正题你认为你不使用的地方不再进口,但它非常很难确定.

  • python 有哪些好的经验法则进口?

    • dbr 的回答包含<块引用>

      不要做 from x import * - 它使你的代码很难理解,因为你不能轻易看到一个方法在哪里来自 (from x import *; from y进口 *;my_func() - my_func 在哪里定义?)

因此,我倾向于达成共识,即通过

明确说明我在模块中使用的所有项目

USE modulename, only : var1, var2, ...

正如Stefano Borini 提到的,

<块引用>

[如果]你的模块太大以至于你觉得有必要只添加,这意味着你的模块太大了.拆分它.

解决方案

这是一个平衡问题.

如果您只使用模块中的一些东西,那么添加 ONLY 是有意义的,以明确指定您正在使用的内容.

如果你使用模块中的很多东西,指定 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:

  1. explicitly declare which variables/functions/subroutines you're using with the , only : syntax, such as USE [module_name], only : variable1, variable2, ...?
  2. 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.

  • 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?)

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆