coding style - How can I define a subprocess in the __init__() part in Python? -


consider following class:

import subprocess  class fruits():     def __init__(self):         self.terminal_width = 80      def start(self):         p = subprocess.popen(["mplayer", "other", "args"])         print "subprocess started..." 

this code works.

to gain more insight in best coding practices, i'm using pep 8 linter python. linter complains line

p = subprocess.popen(["mplayer", "other", "args"]) 

: linter says because we're defining variable (p), should go __init__() method instead.

i'm wondering how this, though. if transfer line __init__() in current form, subprocess start running when fruits() instantiated, not want. can me here?

at first creating local variable. ok, lost when execution of method has finished.

you want have instance variable. line has this:

self.p = subprocess.popen(["mplayer", "other", "args"]) 

but p poor choice of name, should use longer, mplayer_proc example.

then there convention initialize instance variables in __init__, not required helpful when use ide. don't need give finial value. if have nothing store there while initializing, set none:

class fruits():     def __init__(self):         self.terminal_width = 80         self.mplayer_proc = none 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -