MorgoZ
MorgoZ

Reputation: 2052

About the need of creating a class in Python

Let's say that i have a Python module to control a videoconference system. In that module i have some global variables and functions to control the states of the videoconference, the calls, a phone book, etc.

To start the control system, the module self-executes a function to initialize the videoconference (ethernet connection, polling states and so)

Now, if i need to start controlling a second videoconference system, i'm not sure how to approach that problem: i thought about making the videoconference module a class and create two instances (one for each videoconference system) and then initialize both, but the problem is that i don't really need to have two instances of a videoconference class since i won't do anything with those objects because i only need to initialize the systems; after that i don't need to call or keep them for anything else.

example code:

Videoconference.py

class Videoconference:
    def __init__(self):
        self.state = 0
        #Initialization code

Main.py

from Videoconference import Videoconference

vidC1 = Videoconference()
vidC2 = Videoconference()
#vidC1 and vidC2 will never be use again

So, the question is: should i convert the videoconference module to a class and create instances (like in the example), even if i'm not going to use them for anything else appart of the initialization process? Or is there another solution without creating a class?

Upvotes: 0

Views: 53

Answers (3)

Frumples
Frumples

Reputation: 433

I think you're approaching this problem the right way in your example. In this way, you can have multiple video conferences, each of which may have different attribute states (e.g. vidC1.conference_duration, etc.).

Upvotes: 1

David542
David542

Reputation: 110209

Perhaps this is a matter of preference, but I think having a class in the above case would be the safer bet. Often I'll write a function and when it gets too complicated I'll think that I should have created a class (and often do so), but I've never created a class that was too simple and thought that this is too easy why didn't I just create a function.

Even if you have one object instead of two, it often helps readability to create a class. For example:

vid = VideoConference()
# vid.initialize_old_system() # Suppose you have an old system that you no longer use
                              # But want to keep its method for reference
vid.initialize_new_system()
vid.view_call_history(since=yesterday)

Upvotes: 2

Adam Smith
Adam Smith

Reputation: 54213

This sounds like the perfect use case for a VideoConferenceSystem object. You say you have globals (ew!) that govern state (yuck!) and calls functions for control.

Sounds to me like you've got the chance to convert that all to an object that has attributes that hold state and methods to mutate it. Sounds like you should be refactoring more than just the initialization code, so those vidC1 and vidC2 objects are useful.

Upvotes: 1

Related Questions