0

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?

1
  • 2
    Wrap the initialization code in a function and call it twice? Commented Dec 14, 2016 at 20:58

3 Answers 3

2

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

I agree with @Adam smith, the core issue is that your code currently using global state. Don't use the VideoConference class to init global bars, actually store all state related to the video system in an instance of the class.
1

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.).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.