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

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.

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. So, the structure would look something like this.

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.

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. So, the structure would look something like this.

/school/ pencil. Py book. Py Inside book.Py, you'd have something like this 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 be = 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.

Thanks for the response. But what if my read() and burn() methods are hundreds of lines each and I don't want book. Py to be one enormous mess of code?

– Ian Aug 4 at 1:55 Each method is hundreds of lines? Honestly, I'd try to find ways to refactor that so it's a number of shorter functions. That's not always possible, obviously, but I'd still try.

Ideally, you'd want each function to do one thing, and one thing only, and you'd want it to fit on a single 'page' (be that a printed page or a single screen of your editor without scrolling). That said, even if I had some functions that were several hundred lines long, I'd still put them all in the same class. It fits Python's object model better and it's easier to find everything.

– Jonathanb Aug 4 at 2:04 1 Then they probably can be broken up into smaller pieces, leaving you with a whole bunch of small self-contained private methods and small public entry methods read() and burn() that organise the work of the smaller methods. But in any case, I don't personally see that a huge complex method is any easier to understand in a separate file (away from everything else it refers to in the class) than it is in the main file. – Ben Aug 4 at 2:04 Well yeah, even after splitting it up into a bunch of functions it will still be a lot of lines/code.

But from what you say, it sounds like common practice is still to keep them in the same file. I come from a hardware background, so I guess I'm used to seeing code hierarchies being deeper than they are wide/flat – Ian Aug 4 at 2:26 It's still deep, it's just not stored in separate files. The 'full path' to the read function in my example is school.book.read().

Yours is school.book.read.read(). By removing the folder, I've only removed one layer of nesting, but the main thing is I've made it easier for regex searching, debugging, and code organization. – Jonathanb Aug 4 at 3:53.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions