简体   繁体   English

Python模块与类

[英]Python modules vs classes

I have the following directory structure: 我有以下目录结构:

ican/
  __init__.py
  haz/
    __init__.py
    apple.py
    grape/
      __init.py
      grape.py

where apple.py and grape.py have fruit class definitions. 其中apple.pygrape.py有水果类定义。 In another test file, I am trying to load a fruit class via something akin to myapple = ican.haz.apple() or mygrape = ican.haz.grape() . 在另一个测试文件中,我试图通过类似于myapple = ican.haz.apple()mygrape = ican.haz.grape()东西加载一个水果类。

What is the correct directory structure and import structure for loading a class of this sort? 加载这类类的正确目录结构和import结构是什么? I have also tried something like import ican.haz as icanhaz and then calling myapple = icanhaz.apple() . 我也试过像import ican.haz as icanhaz这样import ican.haz as icanhaz然后调用myapple = icanhaz.apple() I am looking to put all of my classes in the same spot and load them via something like ican.haz.<class> . 我希望将我的所有课程放在同一个地方并通过像ican.haz.<class>这样的东西加载它们。

You are confusing two concepts. 你混淆了两个概念。 A python "module" is a file containing python code that can be imported. python“module”是一个包含可以导入的python代码的文件。 A python "class" is something that can be defined in a module. python“类”是可以在模块中定义的东西。

In your example, apple and grape are modules. 在你的例子中, applegrape是模块。 You cannot calls a module by doing something like apple() , it isn't allowed. 您不能通过执行apple()类的操作来调用模块,这是不允许的。 You would need to import the class contained in the module. 您需要导入模块中包含的类。

So say apple.py had a GetApple class defined in it. 所以说apple.py定义了一个GetApple类。 So apple.py looked like this: 所以apple.py看起来像这样:

class GetApple(object):
    def __init__(self):
        print("I have an apple!")

Modules can also have functions, so you could have a GetApple function instead: 模块也可以有函数,所以你可以改为使用GetApple函数:

def GetApple():
    print("I have an apple!")

Modules can also have variables. 模块也可以有变量。 They can have any number of variables, classes, and functions. 它们可以包含任意数量的变量,类和函数。 You can either import them individually (such as with from apple import GetApple , or import the module and access them from the module import apple . But you cannot import and then call a module (at least not in any reasonable way), only the functions or and classes inside it. 您可以单独导入它们(例如from apple import GetApple ,或导入模块并从模块import apple访问它们。但是您无法导入然后调用模块(至少不以任何合理的方式),只有函数或者里面的类。

For your directory structure, you could then run it using any of these approaches: 对于您的目录结构,您可以使用以下任何方法运行它:

>>> from ican.haz.apple import GetApple
>>> GetApple()
I have an apple!

>>> from ican.haz import apple
>>> apple.GetApple()
I have an apple!

>>> import ican.haz.apple
>>> ican.haz.apple.GetApple()
I have an apple!

And, depending on your __init__.py files, possibly also: 并且,根据您的__init__.py文件,可能还有:

>>> import ican.haz
>>> ican.haz.apple.GetApple()
I have an apple!

>>> import ican
>>> ican.haz.apple.GetApple()
I have an apple!

For grape.py , assuming a corresponding class, this sort of thing would work: 对于grape.py ,假设一个相应的类,这种事情会起作用:

>>> from ican.haz.grape.grape import GetGrape
>>> GetGrape()
I have a grape!

To be uniform, either 要均匀,要么

a) apple.py should be one level lower in an apple folder with an init , or a) apple.py应该在带有initapple文件夹中低一级,或者

b) grape.py should be one level higher and not in its own grape folder. b) grape.py应该高一级,而不是在自己的grape文件夹中。

Then your import will be from ican.haz.apple import Apple and from ican.haz.grape import Grape (for option a); 然后你的导入将from ican.haz.apple import Applefrom ican.haz.grape import Grape (用于选项a); or from ican.haz import apple, grape (for option b). 或者from ican.haz import apple, grape (选项b)。

To get the structure you want, go with option b and modify the init under haz . 要获得所需的结构,请使用选项b并在haz下修改init

haz/__init__.py : haz/__init__.py

from .apple import Apple
from .grape import Grape

__all__ = ['Apple', 'Grape']

And you would import it as import ican.haz.* (considered bad style though). 你会把它导入为import ican.haz.* (虽然被认为是糟糕的风格)。 And it is ~mangling the module vs class name~ (not any more since class names corrected). 它是〜修改模块与类名〜(自更正类名以来不再是)。 Better to use one of the lines below with option a or b. 最好使用下面的一行,选项a或b。

Btw, the imports according to your current structure would need to be: 顺便说一句,根据您当前的结构进口需要:

from ican.haz import apple
from ican.haz.grape import Grape

a = apple.Apple()
g = Grape()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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