Python - 由类和实例变量混淆

时间:2015-04-23 14:41:04

标签: python oop instance-variables class-variables

我尝试为unix find-command编写一个小包装器脚本。我搞砸参数传递的东西。你能否就我的错误给我一个暗示?

错误消息是

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)

3 个答案:

答案 0 :(得分:4)

使用对象实例的名称

调用实例成员
inst.prep_shell_commands(inst.extension, inst.search_string, isnt.rel_numbers)

话虽这么说,如果你的方法总是要调用实例变量,那么你应该重写你的prep_shell_commands方法,这样它就不需要参数。

答案 1 :(得分:3)

这是重写您的代码。这个版本并不完美,但它展示了如何定义一个类&amp;如何以通常在Python中完成的方式使用类实例。

FWIW,在将输入数据传递到您的课程之前验证输入数据可能更好,但我想这里没问题。

我没有测试过这段代码,因为我没有所需的目录和文件,但希望它不包含任何可怕的错误。 :)

#!/usr/bin/env python

import os
import subprocess
import sys

class Frep(object):
    def __init__(self, args):
        if len(args) != 3:
            self.usage()

        self.extension = args[0]
        self.search_string = args[1]
        self.rel_numbers = args[2]
        self.commands = []

    def usage(self):
        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):
        print self.extension
        cmd = "find /data/grep_dir -maxdepth 1 -type d -name '[" + self.rel_numbers + "]*'"
        tmp_folderlist = subprocess.check_output(cmd, shell=True).split('\n')

        for d in tmp_folderlist:
            cmd = "find " + d + " -type f -name '*" + self.extension + "' -exec grep -il '" + self.search_string + "' {} \;"
            self.commands.append(cmd)
        print self.commands

    def exec_commands(self):
        for cmd in self.commands:
            os.system(cmd)


def main():
    inst = Frep(sys.argv[1:])
    inst.prep_shell_commands()
    inst.exec_commands()


if __name__ == '__main__':
    main()

答案 2 :(得分:2)

Frep的实例中,列出以下内容:

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

为了能够正确访问这些变量,您需要执行以下操作:

class Frep(object):
    def__init__(self):
        self.extension = ""
        self.search_string = ""
        self.commands = []
        self.rel_numbers = ""

然后,当您在班级中引用这些变量时,可以使用self.variablename

要在课堂外引用它们,您可以使用Frep.extension

相关问题