更新时间:2023-10-27 19:13:34
这是一个平衡问题.
如果您只使用模块中的一些东西,那么添加 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:
, only :
syntax, such as USE [module_name], only : variable1, variable2, ...
?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’
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?
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 ;)