python - Appending strings across classes in python
问题描述
I have two classes and wish to append a string to a list in Table() from Game()
Here is my code:
class Table(object):
def __init__(self):
self.cards = []
class Game(object):
def __init__(self):
Table().cards.append("test")
print(Table().cards)
解决方案
在这种情况下,您需要像这样在Game类中初始化Table类:
class Table(object):
def __init__(self):
self.cards = []
class Game(object):
def __init__(self):
table = Table()
table.cards.append("test")
print(table.cards)
game = Game()
推荐阅读
- c - 计算和打印矩阵的对角线之和
- javascript - 如何在 Angular 中使用 addEventListener 和 postMessage?
- php - 如何将日期、时间和值从单选按钮放入数据库
- c# - 逐个像素地动态绘制和显示,有一些延迟
- npm - 如何从 Nexus oss 代理获取依赖于 github 项目的 npm 包
- django - Django 测试 - 发送包含整数的数组数组
- android - 在 android 中使用改造登录
- json - 我如何在 nunjuncks 中乘以数据?
- swift - 为什么不能在swift中将可变参数标记为inout?
- vue.js - Vue Js 2 / Vue-CLI 3 / 托管时显示空白页