deakolt deakolt - 1 year ago 79
Perl Question

Equivalent to Perl Modulino for Ruby, Python?

I know Perl has a design pattern known as a modulino, in which a library module file can act as both a library and a script. Is there any equivalent to this in Ruby / Python?

I think this design pattern would be very useful for me; I'm writing workers that are fairly short, but also require a script to run them. I think it would be convenient to have this all run from the same place.

Thank you!

Answer Source

Python has __name__:

class MyClass(object):

if __name__ == '__main__':
    print("This will only run if you run the script explicitly, not import it")

If you run python, the print function will run. If you import MyClass from myscript, the print will not.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download