Python Forum
Python Classes or Functions for large scale application ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Classes or Functions for large scale application ?
#1
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!
Reply
#2
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.
Reply
#3
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!
Reply
#4
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?
Reply
#5
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?
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Classes rob101 4 468 Feb-05-2024, 06:51 PM
Last Post: rob101
  How do I properly implement restarting a multithreaded python application? MrFentazis 1 588 Jul-17-2023, 09:10 PM
Last Post: JamesSmith
  Understanding Python classes PythonNewbee 3 1,150 Nov-10-2022, 11:07 PM
Last Post: deanhystad
Sad Python classes PythonNewbee 4 995 Nov-09-2022, 01:19 PM
Last Post: deanhystad
  How to properly scale text in postscript conversion to pdf? philipbergwerf 3 1,084 Nov-07-2022, 01:30 PM
Last Post: philipbergwerf
  Python running only in application Mawixy 2 1,091 Apr-19-2022, 11:38 AM
Last Post: Mawixy
  KeyError: 0 when trying to dedupe and match records at scale Catalytic 1 2,109 Apr-07-2022, 06:34 PM
Last Post: deanhystad
  Inheritance vs Instantiation for Python classes mr_byte31 7 2,789 Oct-14-2021, 12:58 PM
Last Post: mr_byte31
  breaking a large program into functions, not acting as expected Zane217 9 2,932 Sep-18-2021, 12:37 AM
Last Post: Zane217
  How to send data from a python application to an external application aditya_rajiv 1 2,132 Jul-26-2021, 06:00 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020