gpt4 book ai didi

Python - 被类和实例变量混淆

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:04 24 4
gpt4 key购买 nike

我尝试为 unix 查找命令编写一个小包装脚本。我搞砸了参数传递。你能给我一个关于我的错误的提示吗?

错误信息是

Traceback (most recent call last):
File "frep.py", line 43, in <module>
inst.prep_shell_commands(self.extension, self.search_string, self.rel_numbers)
NameError: name 'self' is not defined

这是代码:

import os
import subprocess
import string
import sys


class Frep(object):

extension = ""
search_string =""
commands = []
rel_numbers = ""


def get_params(self):
Frep.no_of_params = len(sys.argv)
if Frep.no_of_params == 4:
Frep.extension = str(sys.argv[1])
Frep.search_string = str(sys.argv[2])
Frep.rel_numbers = str(sys.argv[3])
else:
print "Usage: frep [FILE_EXTENSION] [SEARCH_STRING] [RELEASE_NUMBERS]"
print "Example: frep sql my_search_string [5-6]"
print " "
sys.exit()


def prep_shell_commands(self, ext, ss, reln):
print ext
tmp_folderlist = string.split(subprocess.check_output("find /data/grep_dir -maxdepth 1 -type d -name '["+reln+"]*'", shell=True), '\n')
#tmp_folderlist = string.split(subprocess.check_output("find /data/grep_dir -maxdepth 1 -type d -name '[6-7]*'", shell=True), '\n')
for d in tmp_folderlist:
commands.append("find " + d + " -type f -name '*" + ext +"' -exec grep -il '" + ss +"' {} \;")
print commands


def exec_commands(self, c_list):
for c in c_list:
os.system(c)


inst = Frep()

inst.prep_shell_commands(self.extension, self.search_string, self.rel_numbers)

exec_commands(self.commands)

最佳答案

您通过使用对象实例的名称来调用实例成员

inst.prep_shell_commands(inst.extension, inst.search_string, isnt.rel_numbers)

也就是说,如果您的方法总是要调用实例变量,那么您应该重写您的 prep_shell_commands 方法,使其不接受任何参数。

关于Python - 被类和实例变量混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29826802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com