从类中的第x个函数引用变量

问题描述 投票:0回答:1

我觉得是时候开始上课了。我有一个这样的结构文件。我无法访问init中的变量集,我该怎么办?我的结构是否正确?现在被描述为多个函数的是三个函数,其中2个能够独立工作,所以我认为把它们放在课堂上是一件好事,所以我可以在另一个项目中轻松使用它们。函数在类的末尾执行。

class args:
    def __init__(self):
        args = self.get_args()
        self._old = args.old
        self._new = args.new
        self._xml = args.xml

    @staticmethod
    def get_args():
        pass #too long to bother here

    @property
    def old(self):
        return self._old

    @property
    def new(self):
        return self._new

    @property
    def xml(self):
        return self._xml

class update:
    def __init__(self, print_dict = False):
        self.print_dict = print_dict

    def dict_of_glyph_order(old, new):
        pass

    def update_VTT_g_order(xml, glyph_id_dict, print_dict):
        if self.print_dict:
            print('%03d == %03d' %(int(glyph_id), int(new_id)), end=',  ')

    def save_new_xml(xml, tree):
        pass

    args = args()
    dict_of_glyph_order = dict_of_glyph_order(args.old, args.new)
    tree = update_VTT_g_order(args.xml, dict_of_glyph_order, self.print_dict) #variable called here
    save_new_xml(args.xml, tree)

if __name__ == '__main__':
    update()
python python-3.x
1个回答
0
投票

首先,你缺少类的名称,第二,你在init方法中声明要使用的变量,你只需要在这个方法的parantheses中使用self +你从类外部获得的变量(),如果你想使用类外部的变量,其他辅助方法可能需要声明的其他变量,next,self。为了声明和使用你在类中任何地方使用的任何变量是必需的,你可以拥有只在一个方法中存在的局部变量,如果你需要它们但你只能在init中声明类变量,最终在类中的所有内容必须是在方法,init或其他方法中,类本身不是方法,它更像是您定义的自定义变量tipe,并为其提供构建方法,并且您必须创建已定义的tipe的变量才能使用它的功能。

class Some_name:     #no space allowed just underscore in the name
    def __init__(self,print_dict):
        self.print_dict = print_dict 
    def one_of_multiple_functions(some other variable):
        #the thing you want it to do

示例:

A=Some_name(print_dict)
A.one_of_multiple_functions(some other variable)  #in order to use the method

如果你想要一些更好的axamples尝试SoloLearn,它是一个移动应用程序,教你任何流行的编程语言的基础知识。

© www.soinside.com 2019 - 2024. All rights reserved.