OOP - the organization of large classes

I am starting to write a large python library for some area of ​​math.

So, I have this data structure (defined in class A), which is a mathematical model.

I have a bunch of smaller functions that I put in A:

Af (), Ag (), ...

What more or less "auxiliary functions" are needed to calculate the more important functions AX (), AY (), ... that the end user is interested in.

All of these functions, of course, depend on the data in A.

But the number of methods in is growing, growing and getting confused. How to break such a class into smaller pieces. Suppose a data structure with basic operations in it and "methods" that perform calculations in the structure?

What is the usual approach and the most pythonic approach?

+6
source share
2 answers

You can use a pure functional approach and move methods that class users should not call for an object instance to split files.

In a purely functional approach, functions are independent of any internal state, have no side effects, and calculate the return value based only on the arguments presented.

An example to illustrate will replace the following:

# shape.py class Shape: def __init__(self, x, y): self.x = x self.y = y def area(self): return self.x * self.y 

with:

 # shape.py class Shape: def __init__(self, x, y): self.x = x self.y = y # func.py def area(shape): return shape.x * shape.y 

Of course, it would be nice to extract the area method of the Shape class into a separate function in another file, but you can certainly move all the "helper functions" to separate the files and call them correctly from the class methods.

It will also greatly simplify the testing of ancillary functions.

+4
source

This convention is for the "private" prefix or helper methods that are not part of the public interface of the class with single or double underscores.

One effect is, for example, that methods such as _my_helper are not displayed when help is called in the class.

See this post for more details.

0
source

Source: https://habr.com/ru/post/985983/


All Articles