python - 类中的另一个函数在一个函数中使用局部变量
问题描述
由于某些情况,我只能将参数传递给类中的一个函数。
例子:
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
我想了解是否可以在函数中使用win1
变量want_touse
。
解决方案
在 中定义您的属性__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.
此示例仅使用单个变量。您可以根据需要将其应用于其他变量。
推荐阅读
- data-recovery - Windows分区期间的数据丢失| 最重要的验尸工具
- bash - 使用 bash 剪切字母
- javascript - 在没有 npm 的情况下使用 Redux
- javascript - 第一个空格处的 JSON 拆分值
- flutter - 从广播接收器访问相同的 FlutterEngine / FlutterChannel
- graph - 在 Azure Graph Explorer 中看不到我的通讯组
- android - 如何控制EditText?
- kotlin - 写入同一个文件的协程
- inheritance - 在 Rails 5 中,如何从我定义的模块继承?
- dynamic-programming - 这个最优子结构方程正确吗?