[英]Python Module Imports
I am importing a module 我正在导入一个模块
from Boards import CheckerBoardModule
CheckerBoardModule
includes a class called CheckerBoard
CheckerBoardModule
包含一个称为CheckerBoard
的类
When I want to use it, I need to call CheckerBoardModule.CheckerBoard
, rather than just CheckerBoard
. 当我想使用它时,我需要调用
CheckerBoardModule.CheckerBoard
,而不仅仅是CheckerBoard
。 eg 例如
class SnakesAndLaddersBoard(CheckerBoardModule.CheckerBoard):
def __init__(self,width,canvas):
CheckerBoardModule.CheckerBoard.__init__(self,width,canvas)
self.snakes = []
self.ladders = []
Why can't I just used CheckerBoard
, without the CheckerBoardModule
prefix? 为什么没有
CheckerBoardModule
前缀就不能使用CheckerBoard
?
Because you imported the CheckerBoardModule
module object, not the CheckerBoard
class. 因为您导入的是
CheckerBoardModule
模块对象, 而不是 CheckerBoard
类。 If you want to use just CheckerBoard
, import that : 如果你想使用只是
CheckerBoard
上, 导入 :
from Boards.CheckerBoardModule import CheckerBoard
All that importing does, apart from executing the module if that hasn't already happened, is bind the imported name in your own namespace. 除了执行模块(如果尚未执行)之外,导入所做的全部工作是将导入的名称绑定到您自己的名称空间中。
from foo import bar
binds bar
to whatever bar
was bound to from foo
. from foo import bar
将bar
绑定到foo
绑定的任何bar
。 from foo import bar as baz
lets you specify a new name. from foo import bar as baz
允许您指定新名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.