Reputation: 387
I have this function:
def ReadThis(TextToRead):
speak = wincl.Dispatch("SAPI.SpVoice")
speak.Speak(TextToRead)
I have two ways to go about it:
1 - Create the Speak object once, outside the function, and then use it again and again.
2- Create the object each time I call the function.
I'm trying to figure out:
A) Would it be much less efficient if I'll create a new object every time I call the function?
B) Should I delete/close the object at the end of the procedure?
Thanks...
Upvotes: 0
Views: 614
Reputation:
Creating an object is a (relatively) expensive process in terms of the time required to do so, as compared to calling a function for example, especially if this object is complex. If this method is going to be used repeatedly then it should be declared outside of the function, as this would be more efficient. This all depends on what your requirements are for your program of course. The garbage collector will take care of the object it is no longer needed in your program, so this shouldn't be a problem. I hope this helps.
Upvotes: 1