Python Forum

Full Version: Python Classes or Functions for large scale application ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
We are developing large scale project in python, But we are stuck in deciding on whether to implement project using classes or just modules with functions or even using both (Functions to util methods and classes to Features).

What is the best practice to develop large scale applications according to python? (Considering the performance, Best use of python)

Thanks!
Classes keep related functions organized into one box. The objects held in that box are functions, but since there are within the scope of the class, they now become 'methods' by name.

Everything within a class, is capable of knowing everything else, that's within the class, and easily interacting with the other components.

when you import a class, you don't only get the object that you are interested in, but all supporting methods as well. If you only intend to use a portion of that, you can use 'from' ... import ...

I like to compare classes vs functions to an automobile service garage.

A function is a tool. A class is a chest of tools, all organized by their use for a particular function. It's easier to get a brake tool in the brake tool chest, rather than searching the garage for that same tool.

I personally would never consider writing a large project without classes. But that's my personal preference, and I believe it makes simple sense.
if I implement my project with classes then when I need to access dependencies of class, I need to import them and instantiate them within class __init__ method as below.
    def __init__(self):
        self.x = X()
        self.y = Y()
        self.z = Z()
        self.a = A()
        self.b = B()
        self.c = C()
In this case I had to instantiate multiple objects for one class likewise there will be multiple classes creating multiple objects.
Is that okay? Considering memory usage, garbage collection, performance?

Thanks!
No, this is not how you instantiate multiple instance of a class, see https://python-forum.io/Thread-Classes-Class-Basics

EDIT: Or (after second reading) maybe I don't get your question right?
My code looks like this.

from wq import x
from ew import y
from fa import z
from oh import a
from la import b
from ha import c

class Sample: 
    def __init__(self):
       self.x = X()
       self.y = Y()
       self.z = Z()
       self.a = A()
       self.b = B()
       self.c = C()

   def calculate_something(self):
       self.answer = self.a.getValue() + self.b.getAnswer() + self.c.getOptions() + self.x.getVehichles()

   ....
Likewise I have to import all required classes from other sub directories and create an instance of them within my Sample class __init__. Here I had to create objects 6 objects of 6 classes. Then SampleTwo class has similar set of dependencies and it will also import around 4 classes and create instance of that within SampleTwo class's __init__ method.

So when the project grows larger, more number of objects will be created. Is that okay? Considering the garbage collection, memory usage of the application, etc?
Or is there any better way to do that?
do you have six different classes? if so, why? If all are related they should be in same class.
you need to read https://python-forum.io/Thread-Classes-Class-Basics as buran suggested.