Py学习  »  Python

python无法访问其他方法中的类方法[重复]

aditya rawat • 7 年前 • 2354 次点击  

这个问题已经有了答案:

我一直在创建一个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_unhide 类中的其他方法无法访问。

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

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/37916
文章 [ 1 ]  |  最新文章 7 年前
Daniel Roseman
Reply   •   1 楼
Daniel Roseman    8 年前

这种“不寻常的行为”一点也不奇怪,但恰恰是Python的工作方式。您总是需要通过以下方式引用方法和属性: self .

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