0

I have a class which should call subprocess.call to run a script as part of it's job.

I've defined the class in a module and at the top of the module I have imported the call function from subprocess.

I am not sure whether I should just use call() inside one of the functions of the class or whether I should make it a data member and then call it via the self.call function object.

I would prefer the latter but I'm not sure how to do this. Ideally I would like not to have subprocess at the top and have something like:

class Base:
    def __init__():
        self.call = subprocess.call

but the above doesn't work. How would you go about doing this? I'm very new to Python 3.

2
  • 6
    Why do you need to store the call function at all? Commented Nov 2, 2012 at 16:35
  • 2
    How does this fail for you? Base().call('echo') works for me. Did you mean to forget to specify the self parameter in the __init__(self) method signature? Commented Nov 2, 2012 at 16:52

1 Answer 1

1

Do you mean that you want:

import subprocess

class Base:
    def __init__(self):  #don't forget `self`!!!
        self.call = subprocess.call

instance = Base()
print (instance.call(['echo','foo']))

Although I would really prefer:

import subprocess

class Base:
    call = staticmethod(subprocess.call)
    def __init__(self):
        self.call(["echo","bar"])

instance = Base()
print (instance.call(['echo','foo']))

Finally, if you don't need to have call as part of your API for the class, I would argue it's better to just use subprocess.call within your methods.

Sign up to request clarification or add additional context in comments.

1 Comment

You could also use __call__ = staticmethod(subprocess.call) and then instance(['echo','foo'])

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.