python - 重用在 python 类中定义的变量
问题描述
假设我定义了一个类。我有一个有趣的方法,我创建 self.x。我想以不同的方法(重用)使用 self.x。我怎么能那样做?因为我尝试了我写的东西并且没有工作。谢谢!
class test:
def __init__(self,t):
self.t = t
def fun(self):
self.x = self.t+1
def reuse(self):
self.y = self.x
解决方案
您需要在__init__
方法中创建所有变量。尝试这个:
class test:
def __init__(self,t):
self.t = t
self.x = 0
self.y = 0
def fun(self):
self.x = self.t+1
def reuse(self):
self.y = self.x
推荐阅读
- c# - 在 Devexpress Gridcontrol 绑定中使用 nameof 指定 FieldName
- php - 文件导出到空白 excel - PHP
- javascript - 将引导程序下拉默认文本替换为所选选项的文本
- jquery - 将折叠按钮添加到列表
- api - OVER_QUERY_LIMIT 地理编码
- css - 如何在 Header 下放置不同风格的阴影?
- ios - 自定义 segue 到导航控制器层次结构中的特定子视图
- windows - 组装 如何将 ntoskrnl 绑定到我的汇编语言对象?
- php - Laravel 5 表单模型绑定单选按钮
- assembly - 有人能告诉我我的 MIP 翻译成英文好不好?