简体   繁体   English

Python模块导入

[英]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 barbar绑定到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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM