简体   繁体   English

如何针对该类的帮助文件正确组织Python类定义?

[英]How to properly organize a Python class definition with respect to helper files for that class?

This has probably been asked before but I couldn't find the answer to my specific question (fairly general...) 这可能是之前被问过但我找不到我的具体问题的答案(相当一般......)

Here is an example of my question. 这是我的问题的一个例子。 Say my package is called 'school', and I have a class called 'book' which will have .py files along side it containing the meat of its methods. 假设我的包名为'school',我有一个名为'book'的类,其中包含.py文件,其中包含其方法的内容。 I'm not sure how to organize it all so that import statements don't look strange. 我不确定如何组织它以使import语句看起来不奇怪。

How do I organize files? 我如何组织文件?

/school/
        pencil/
        book/
             __init__.py
             read.py
             burn.py
             book.py

I want to be able to do something like this, since it makes the most sense: 我希望能够做到这样的事情,因为它最有意义:

from school import Book
b = Book(name="The Bible")
b.read()

But from the file structure above, I would have to do: 但是从上面的文件结构中,我必须这样做:

from school.book import Book
b = Book(....etc

OR 要么

from school import book
b = book.Book(...etc

These are awkward/repetitive...what am I missing here? 这些都很笨拙/重复......我在这里错过了什么?

You're confusing packages with classes I think. 您认为包与我们认为的类混淆。 Personally, I'd put every class definition and all functions that were directly related to that class in the same .py file. 就个人而言,我将每个类定义和与该类直接相关的所有函数放在同一个.py文件中。 For instance, reading is not an object, so I would put that as a function under the Book class, not it's own .py file. 例如,读取不是一个对象,所以我将它作为一个函数放在Book类下,而不是它自己的.py文件。 So, the structure would look something like this. 所以,结构看起来像这样。

/school/
    pencil.py
    book.py

Inside book.py, you'd have something like this 在book.py里面,你会有这样的东西

class Book():
    def __init__(self,name,author,isbn,your_variable_here):
        #Your init method

    def read(self,kid):
        return "{0} is reading {1}.".format(kid,self.name)

    def burn(self,library,lighter):
        library.remove(self)
        lighter.light(self)
        return "Preparing to burn people."

Then, your imports look like this. 然后,您的导入看起来像这样。

from school import book
    b = book.Book("The Art of War","Sun Tzu",'999342052X','Books rock!')
    b.read(ike) #This assumes ike is an object, probably of the class Student, defined and imported from elsewhere
    b.burn(library,lighter) #Once more, I'm assuming these are objects for which you've imported the definition and defined them earlier.

This advantage of this system is it more closely models reality. 这个系统的这个优点是它更接近现实的模型。 Rather than a bunch of functions bundled by a file structure (which, as you noted, can get convoluted), you've got the grouped by classes into logical groups and constructs. 而不是由文件结构捆绑的一堆函数(正如您所指出的那样,可以进行复杂化),而是按类将其分组为逻辑组和构造。 However, I'd argue that Student should have the read function and library should have the checkout function, leaving books with only the burn function. 但是,我认为学生应该具有读取功能,并且库应该具有结帐功能,只留下具有刻录功能的书籍。 But that's because books don't read, people do. 但那是因为书不读,人们会这样做。 And books don't check out, libraries do. 书籍不会检查,图书馆也可以。 That's a question of how you want to organize it. 这是一个如何组织它的问题。

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

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