Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
classes question
#1
I have been developing an app for retrieving records from a database (SQL, tkinter, windows).
Over time I have externalised 8 functions into 8 classes . "Single-purpose" functions like creating the tkinter Gui, etc.
Why ? Readability, maintainability etc...etc...

I could do at least 5 or 6 more classes . (Like handling the SQL query & results...)
My question: will the growing number of external classes slow down the app ?
Are there things that should definitely not be in a class ?
Just wondering.
thx,
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#2
(Jul-25-2024, 07:37 AM)DPaul Wrote: My question: will the growing number of external classes slow down the app ?
Are there things that should definitely not be in a class ?
For the first question the answer is no. There should be no significant impact unless you instantiate hundreds of thousands of objects.

For the second question, I'd say no too, but it is more a personal opinion. Some people advocate to write more functions and less classes. These discussions often look like a waste of time to me.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
(Jul-25-2024, 07:37 AM)DPaul Wrote: My question: will the growing number of external classes slow down the app ?

In general: All abstractions costs RAM or CPU.

Yes, it slows down the app in a range of microseconds.
We don't care about this, until we call a method a million times.
But the same for functions or massive iterations.

import time

class Foo:
    pass

start = time.perf_counter()

for _ in range(1_000_000):
    Foo()

duration_us = (time.perf_counter() - start)
print(f"Creating new instances took {duration_us:.3f} µs per instance.")
This was on a Raspberry Pi Zero W Rev 1.1
Output:
Creating new instances took 4.203 µs per instance.
Just don't do stupid things like creating an instance of a class in a loop, if the instance could be reused by the loop.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
OK guys, Thanks.
You just gave me a truckload of extra work.
If you would have said "no, don't make more classes", I would have been
watching the oplympics, now I'm coding. Sad
Paul
Larz60+ likes this post
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#5
Find some time for the Olympics, they are ending soon Big Grin
Reply


Forum Jump:

User Panel Messages

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