开发者

Global name in self/class?

开发者 https://www.devze.com 2023-01-31 16:57 出处:网络
I am having issues calling variables in my class. I have everything set up as self\'s and what have but i am still getting errors. I think i am having a hard time figu开发者_开发技巧ring this out caus

I am having issues calling variables in my class. I have everything set up as self's and what have but i am still getting errors. I think i am having a hard time figu开发者_开发技巧ring this out cause i am new to 3.0 scripting.

Here is my script:

http://pastebin.com/9Lrw399E

here is the error:

command = 'tcpdump -c5 -tttt -w {0} host {1}'.format(raw, input_host)
NameError: global name 'raw' is not defined

if i make them self.raw or self.input_host

it get this:

command = 'tcpdump -c5 -tttt -w {0} host {1}'.format(self.raw, self.input_host)
AttributeError: 'MainLoop' object has no attribute 'raw'


command = 'tcpdump -c5 -tttt -w {0} host {1}'.format(raw, input_host)

Should be:

command = 'tcpdump -c5 -tttt -w {0} host {1}'.format(self.raw, self.input_host)

Notice the self.


try

command = 'tcpdump -c5 -tttt -w {0} host {1}'.format(self.raw, self.input_host)

Unless you're passing raw and input_host in as function parameters, you need to use self.variable to look up the variable for the class instance.

Edit: You'll also need to make sure that whatever functions define self.raw and self.input_host are called before this line of code is run. From your code, if you call MainLoop.cmd(), you must call MainLoop.host() AND MainLoop.inputname() before cmd() so that self.raw and self.input_host exist in the instance of the class.

In this case, you should probably create a constructor for your class that at least creates the instance variables

class MainLoop:
    def __init__(self):
        self.raw = None
        self.input_host = None

and then check the value of self.raw and self.input_host before creating the command.

def cmd(self):
    if self.raw is not None and self.input_host is not None:
        command = 'tcpdump -c5 -tttt -w {0} host {1}'.format(self.raw, self.input_host)
        subprocess.call(command.split(), shell=False)
0

精彩评论

暂无评论...
验证码 换一张
取 消