有没有办法在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)
:
从\uuuu init\uuuuuuuu上的ClassName创建实例时,缺少定义的结尾和必需的参数
List
和range
创建类实例printclass()
到底是什么不起作用?您的代码的期望和实际输出是什么?非常感谢…现在代码可以工作了