自我/班级中的全局名称?
我在类中调用变量时遇到问题。我已将所有内容设置为自己的内容,但我仍然收到错误。我想我很难弄清楚这一点,因为我是 3.0 脚本编写的新手。
这是我的脚本:
这是错误:
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该是:
注意
self
。Should be:
Notice the
self
.除非您将 raw 和 input_host 作为函数参数传递,否则
您需要使用 self.variable 来查找类实例的变量。
编辑:您还需要确保在运行此行代码之前调用定义 self.raw 和 self.input_host 的任何函数。在您的代码中,如果您调用
MainLoop.cmd()
,则必须调用MainLoop.host()
ANDMainLoop.inputname()
在cmd()
之前,以便self.raw
和self.input_host
存在于类的实例中。在这种情况下,您可能应该为您的类创建一个构造函数,该构造函数至少创建实例变量
,然后在创建命令之前检查 self.raw 和 self.input_host 的值。
try
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 callMainLoop.host()
ANDMainLoop.inputname()
beforecmd()
so thatself.raw
andself.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
and then check the value of self.raw and self.input_host before creating the command.