如何在 python 中访问不同类中任何类的变量和函数


import copy
class Myclass0:
paperlist=[]
class Myclass1:
def copy_something(self):
Paper = Myclass0()
Flowers = ["Roses","Sunflower","Tulips","Marigold"]
Paper.paperlist = copy.copy(Flowers)
class Myclass3:
superlist = []
Paper = Myclass0()
print(Paper.paperlist)
superlist.append(paperlist[0])     

我在编译时收到超出范围的索引错误。请帮助我找到一种方法,使用类 Myclass1 函数和属性在 Myclass3 中打印Myclass0纸质列表您可以更改类主体,但应使用"所有类"。

我在等待你的宝贵努力。

谢谢

也许这个代码片段可以帮助你更好地理解它:

class MyClass0:
def __init__(self):
# this is now an attribute of the instance (not the class)
self.paperlist = []

class MyClass1:
@staticmethod
def copy_something(paper):
# this is a static method (it doesnt rely on the Class (MyClass1) or an instance of it
flowers = ["Roses", "Sunflower", "Tulips", "Marigold"]
paper.paperlist = flowers

class Myclass3:
def __init__(self, paper):
# when an instance of this class is created an instance of MyClass0
# must pre passed to its constructor. It then prints out its paperlist
print(paper.paperlist)

paper = MyClass0()
MyClass1.copy_something(paper)
Myclass3(paper)

最新更新