类中的另一个函数在函数中使用局部变量

时间:2018-10-25 12:09:36

标签: python class variables

由于某些情况,我只能将参数传递给类中的一个函数。

示例:

class win1():
    def __init__(self):
        self.diction = dict()
        self.x = self.wanted_touse()
        #is it possible to make it here? If yes, it will be good to be like this
        # self.X = X or self.y = Y (is this even possible without passing an argument in it?)

    def fun(self, X,Y):
        self.x = X
        self.y = Y

    def wanted_touse(self):
        #I wanted to use X and Y here while at the same time I cannot pass the argument to this function because of some circumstances.
        #Here with some functions to make the dictionary for self.x example for x in enumerate(self.x)

        self.diction[something] = something

我想学习看看是否可以在win1want_touse函数中的函数中使用变量。

1 个答案:

答案 0 :(得分:1)

__init__()中定义属性,然后在fun()函数中对其进行修改,如下所示:

class win1():
    def __init__(self):
       self.x = None

    def fun(self, X,Y):
        self.x = X

    def wanted_touse(self):
        pass
        #do whatever you want with self.x here, so long as you've run your fun() function and changed self.x as you intended.

此示例仅使用单个变量。您可以根据需要将其应用于其他变量。