可以将实例命名为与模块相同的名称吗?
Is it OK to name an instance the same as a module?
我经常发现 'natural' 以小写形式命名包含 class 定义的模块和该 class 的实例,并使用驼峰式命名 class 名字。例如,我想这样做:
In [2]: from publisher import Publisher
In [3]: publisher = Publisher()
这里我有一个名为 publisher
的模块,还有一个以相同方式调用的 Publisher
实例。看起来模块和实例都如预期的那样'work':
In [4]: from publisher import RandomData
In [5]: publisher.random.uuid()
Out[5]: 'c490508d-2071-536e-2f38-4b03b04351e1'
我从模块中导入了另一个 class 并调用了一个实例方法。 Python 'understand from the context' 是模块还是实例?可以这样重复使用名字吗?
你没有隐藏任何东西,这里没有重复使用任何名称。在您使用 publisher = Publisher()
创建该实例之前,名称 publisher
未在您的命名空间中使用 。如果您尝试在 from publisher import Publisher
行之后立即使用名称 publisher
,您会得到一个 NameError
异常。
那是因为 from <module> import <name>
形式只在您的命名空间中设置 <name>
。 <name>
从哪里导入并不重要;您绝不会在您的命名空间中获得 <module>
名称。
换句话说,from publisher import Publisher
语句基本上转换为:
if 'publisher' not in sys.modules:
# find and load the publisher module
# sys.modules['publisher'] = newly_loaded_module
Publisher = sys.modules['publisher'].Publisher # set the Publisher global
除了名称 sys
从未在您的命名空间中设置,Python 只是在内部直接访问 sys.modules
。
所以,从技术角度来看:不,这完全没问题。
您 可能 发现使用模块名称作为实例变量可能会导致混淆名称所指的是什么 未来 reader你的代码,如果不是Python。
您可能还把它与
混淆了
import publisher
publisher = publisher.Publisher()
这可能 会 隐藏模块。 import publisher
行设置全局名称 publisher
,在下一行 将 publisher
替换为新的对象引用。
转换成相同的 sys.modules
语言,你会这样做:
if 'publisher' not in sys.modules:
# find and load the publisher module
# sys.modules['publisher'] = newly_loaded_module
publisher = sys.modules['publisher'] # set the publisher global
publisher = publisher.Publisher() # set the publisher global to something else
这也很好,除非您希望 publisher.Publisher()
稍后再次工作。 publisher
不再引用该模块,因此可能会导致问题。这对人类 readers 来说更令人困惑。
我经常发现 'natural' 以小写形式命名包含 class 定义的模块和该 class 的实例,并使用驼峰式命名 class 名字。例如,我想这样做:
In [2]: from publisher import Publisher
In [3]: publisher = Publisher()
这里我有一个名为 publisher
的模块,还有一个以相同方式调用的 Publisher
实例。看起来模块和实例都如预期的那样'work':
In [4]: from publisher import RandomData
In [5]: publisher.random.uuid()
Out[5]: 'c490508d-2071-536e-2f38-4b03b04351e1'
我从模块中导入了另一个 class 并调用了一个实例方法。 Python 'understand from the context' 是模块还是实例?可以这样重复使用名字吗?
你没有隐藏任何东西,这里没有重复使用任何名称。在您使用 publisher = Publisher()
创建该实例之前,名称 publisher
未在您的命名空间中使用 。如果您尝试在 from publisher import Publisher
行之后立即使用名称 publisher
,您会得到一个 NameError
异常。
那是因为 from <module> import <name>
形式只在您的命名空间中设置 <name>
。 <name>
从哪里导入并不重要;您绝不会在您的命名空间中获得 <module>
名称。
换句话说,from publisher import Publisher
语句基本上转换为:
if 'publisher' not in sys.modules:
# find and load the publisher module
# sys.modules['publisher'] = newly_loaded_module
Publisher = sys.modules['publisher'].Publisher # set the Publisher global
除了名称 sys
从未在您的命名空间中设置,Python 只是在内部直接访问 sys.modules
。
所以,从技术角度来看:不,这完全没问题。
您 可能 发现使用模块名称作为实例变量可能会导致混淆名称所指的是什么 未来 reader你的代码,如果不是Python。
您可能还把它与
混淆了import publisher
publisher = publisher.Publisher()
这可能 会 隐藏模块。 import publisher
行设置全局名称 publisher
,在下一行 将 publisher
替换为新的对象引用。
转换成相同的 sys.modules
语言,你会这样做:
if 'publisher' not in sys.modules:
# find and load the publisher module
# sys.modules['publisher'] = newly_loaded_module
publisher = sys.modules['publisher'] # set the publisher global
publisher = publisher.Publisher() # set the publisher global to something else
这也很好,除非您希望 publisher.Publisher()
稍后再次工作。 publisher
不再引用该模块,因此可能会导致问题。这对人类 readers 来说更令人困惑。