有没有办法在python中循环这些元素的函数调用?

有没有办法在python中循环这些元素的函数调用?,python,function,loops,class,call,Python,Function,Loops,Class,Call,我想循环这些元素的函数调用…可能吗 class ClassName: def __init__(self, property): self.property = property def printclass(self): print(self.property) e1 = ClassName(...) e1.printclass() e2 = ClassName(...) e2.printclass() e3 = ClassName(

我想循环这些元素的函数调用…可能吗

 class ClassName:
      def __init__(self, property):
        self.property = property
      def printclass(self):
        print(self.property)

e1 = ClassName(...)
e1.printclass()
e2 = ClassName(...)
e2.printclass()
e3 = ClassName(...)
e3.printclass()
...
这就是我试图做的…它不起作用

elements = [e1, e2, e3,...]

for x in elements:
  print(x.printclass())

这些只是一些注释…不是代码

如果我没有误解你的问题,那么这是一种方法-

class ClassName:
    def __init__(self, property):
        self.property = property
    def printclass(self):
        print(self.property)
    
instances = [ClassName('send_property_here') for i in range(10)]
for e in instances:
    print(e.printclass)
  • 首先修复现有代码中的一些打字错误。e、 g缺少
    \uuuu init\uuuuuuuu上的ClassName创建实例时,缺少
    定义的结尾和必需的参数
  • 您可以使用
    List
    range
    创建类实例
  • 迭代该实例列表并调用
    printclass()

  • 到底是什么不起作用?您的代码的期望和实际输出是什么?非常感谢…现在代码可以工作了