Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python无法访问其他方法中的类方法_Python_Class - Fatal编程技术网

Python无法访问其他方法中的类方法

Python无法访问其他方法中的类方法,python,class,Python,Class,我一直在创建一个python脚本,它基本上隐藏/取消隐藏linux环境中的文件和文件夹。这是密码 import os class unhide_files: def __init__(self, directory): self.parent_dir,self.sub_folders,self.files = list(os.walk(directory))[0] os.chdir(directory) def general_unhide(

我一直在创建一个python脚本,它基本上隐藏/取消隐藏linux环境中的文件和文件夹。这是密码

import os


class unhide_files:
    def __init__(self, directory):
        self.parent_dir,self.sub_folders,self.files = list(os.walk(directory))[0]
        os.chdir(directory)

    def general_unhide(self,files):
        try:
            for f in files:
                if "." in f:
                    os.rename(f,f.replace(".","",True))
        except Exception as err:
            print(str(err))

    def unhide_file(self):
        general_unhide(self.files) #unhide_file could not use general_unhide


    def unhide_sub_folders(self):
        general_unhide(self.sub_folders) #unhide_sub_folders could not use general_unhide


    if __name__ == "__main__":
        path = input("Enter the directory's path ")
        unhid = unhide_files(path)

        # testing to see if it works. Directory unhiding is still to 
        be done
        unhid.unhide_file()
        # hiding files behaviour is still to be implemented
代码的问题在于,除了作为类方法之外,类中的其他方法无法访问
general\u unhide

有什么特别的原因吗?或者这只是一个愚蠢的错误?

这种“不寻常的行为”一点也不奇怪,但正是Python一贯的工作方式。您始终需要通过
self
引用方法和属性

def unhide_file(self):
    self.general_unhide(self.files)

类上的方法不是全局的。改用
self.
引用同一对象上的方法。