自我/班级中的全局名称?

发布于 2024-10-08 07:35:55 字数 558 浏览 3 评论 0原文

我在类中调用变量时遇到问题。我已将所有内容设置为自己的内容,但我仍然收到错误。我想我很难弄清楚这一点,因为我是 3.0 脚本编写的新手。

这是我的脚本:

http://pastebin.com/9Lrw399E

这是错误:

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

如果我自己制作。 raw 或 self.input_host

它得到这个:

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

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 figuring 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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒解孤独 2024-10-15 07:35:55
command = 'tcpdump -c5 -tttt -w {0} host {1}'.format(raw, input_host)

应该是:

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

注意self

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.

熟人话多 2024-10-15 07:35:55

除非您将 raw 和 input_host 作为函数参数传递,否则

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

您需要使用 self.variable 来查找类实例的变量。

编辑:您还需要确保在运行此行代码之前调用定义 self.raw 和 self.input_host 的任何函数。在您的代码中,如果您调用 MainLoop.cmd(),则必须调用 MainLoop.host() AND MainLoop.inputname() cmd()之前,以便self.rawself.input_host存在于类的实例中。

在这种情况下,您可能应该为您的类创建一个构造函数,该构造函数至少创建实例变量

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

,然后在创建命令之前检查 self.raw 和 self.input_host 的值。

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文