1

Experimenting with ex43.py (make a game) from LearnPythonTheHardWay and using time.sleep() to create pauses between events. In order to enable and disable the feature, or change the length of the pauses, I am just holding a variable in class Engine(), which is referenced from the calls to time.sleep() throughout the program.

So:

import time

class Engine(object):

    sleep1 = 1

    #some methods
    def say_hello(self, exit):
        self.exit = exit
        print "Hello"
        time.sleep(Engine.sleep1)
        self.exit.say_goodbye()

class Another(object):

    def say_goodbye(self):
        print "ok"
        time.sleep(Engine.sleep1)
        print "goodbye."

me = Engine()
bye = Another()
me.say_hello(bye)

The question is, if I want time.sleep(1) to be available to multiple methods of various classes, does it need to be passed to each method that needs it as a parameter like this:

import time

class Engine(object):

    sleep1 = 1

    #some methods
    def say_hello(self, exit):
        self.exit = exit
        print "Hello."
        time.sleep(Engine.sleep1)
        pause = time.sleep
        self.exit.say_goodbye(pause)

class Another(object):

    def say_goodbye(self, pause):
        self.pause = pause
        print "ok"
        self.pause(Engine.sleep1)
        print "goodbye."

me = Engine()
bye = Another()
me.say_hello(bye)

And in order to have just one place in which to set the length of the pause, is there an improvement on simple calling time.sleep(Class.var) from whatever method?

MikeiLL
  • 6,282
  • 5
  • 37
  • 68

2 Answers2

1

From what I've seen, I don't get the advantage of having sleep1 inside a class, except if you want to change it during the game.

Passing time.sleep(1) around looks quite tedious. I would say that calling time.sleep(Class.var) every time is OK and makes the code clearer.

I also tested your code and passing the pause doesn't add any advantage to it:

  • the code is harder to read
  • you need to call it by doing this self.pause(Engine.sleep1) which is a strange alias for time.sleep(Engine.sleep1). The one second is not remembered, so you have to input it again to time.sleep() EDIT BELOW
  • you have more source lines of code which seem obscure, making it a lot harder to maintain

Keep it simple. time.sleep(Engine.sleep1) is good.

One small addition: if you only wait on specific functions, calling time.sleep() is perfectly fine. But if you're doing this after each function, maybe you could do some kind of "action queue" and call time.sleep() after each function has completed. So that if your logic changes, you can update one single line of code.

EDIT: To keep the function and it's parameters for time.sleep(), I would do the following:

MY_SLEEP = 1

def my_pause():
    time.sleep(MY_SLEEP)

my_pause() # waits 1 second
my_pause() # waits 1 second again.

but then again, you could simply:

time.sleep(MY_SLEEP)

each time...

EDIT 2 Here I somewhat combined the idea with the static method and the printing into a say()function.

import time

MY_SLEEP = 1 # inside a config file

class Engine(object):

    @staticmethod
    def say(text):
        print(text)
        time.sleep(MY_SLEEP)

    #some methods
    def say_hello(self, exit):
        Engine.say("Hello")
        exit.say_goodbye()

class Another(object):

    def say_goodbye(self):
        Engine.say("ok")
        Engine.say("goodbye")

me = Engine()
bye = Another()
me.say_hello(bye)

When I was talking about an action queue, it was more like a list containing which class/function/arg to call at each round. Then a loop would pop one element from the queue at each turn, do the action, wait one second and start again.

achedeuzot
  • 4,164
  • 4
  • 41
  • 56
  • Thank you for the tip about just creating SLEEP1 outside of a class to make it globally accessible (and naming with CAPS). But the question is really, Does a method have to be passed as a parameter to be available within another classes method? And secondly is there any way to attach a method AND it's parameter together for example `var = time.sleep(1)` (which i know won't work because it just references the return value of the function) – MikeiLL Jul 04 '14 at 23:57
  • If it's an instance method, you'll have to pass it as a parameter to be available within another class I think. If it's a `@staticmethod`, you can call it without passing it as a parameter ( see http://stackoverflow.com/questions/735975/static-methods-in-python ) – achedeuzot Jul 05 '14 at 00:00
  • About your second question, the way I would do it is define a function called `my_sleep()` in which I have `time.sleep(1)`. So everytime I call my function, it sleeps for 1 second. See edit above (in answer) – achedeuzot Jul 05 '14 at 00:02
  • I like the `my_pause()` as a static method - since this is an `OOP` exercise. ` @staticmethod def my_pause(): time.sleep(Engine.sleep1)` Would you be up for adding that to your answer before I accept it? Mostly what I'm using it for is just to delay time between sentences being printed out. Would a function that would accept a paragraph string and insert `time.sleep` based on `\n` or number of words be kind of what you're talking about with an "action queue"? – MikeiLL Jul 05 '14 at 00:22
1

An approach that I use is to create a module with all constants and simple helper methods that might be needed by other modules, classes and their methods, so it's not even a class, e.g. a helper.py module can look like this:

var1 = 1
var2 = 2
def method1():
    ...
def method2():
    ...

I would put that helper.py to a library, say mylib, and then use it from other modules, classes and executable like this:

from mylib import helper
print helper.var1
helper.method1()
helper.method2()

Needless to say that I don't need to instantiate anything with this approach.

Oleg Gryb
  • 5,122
  • 1
  • 28
  • 40
  • Cool. That's pretty simple. Or I could simply put the function outside of a class and either way it can easily be called from within a method. Awesome. This stuff really isn't all that complicated, is it? Maybe someday I'll even understand threads... – MikeiLL Jul 05 '14 at 00:28