我正在构建一个函数流水线,我需要知道每个函数的索引被打开了多少个类实例,我还需要将初始化计数器的'Start'param设置为0,因为它可以处理多次运行同一个流水线而不会使计数增加超过极限。
首先,我建立了一个方法,这个方法只适用于一个管道,因为Counter是和其他管道类共享的。
class Foo3():
class Counter:
def __init__(self,func):
self.counter = 0
self.func = func
#Counter.method =method
def __call__(self,*args, **kwds):
self.counter += 1
return self.func(*args, **kwds)
#count= Counter()
string = 'this is '
start = True
def __init__(self, name):
self.name = name
#self.method = method
self.ainit(self)
self.newinst()
print(self.newinst.counter)
@Counter
def newinst():
pass
@classmethod
def ainit(cls,inst):
print(cls.string +inst.name)
@classmethod
def getCount(cls,inst):
print(self.newinst.counter)
class pipe1(Foo3):
pass
class pipe2(Foo3):
pass
pipe1('test')
pipe1('test2')
pipe1('test3')
pipe2('test_new')
产出
this is test
1
this is test2
2
this is test3
3
this is test_new
4
现在我试图弄清楚如何在Counter类中传递一个start方法来指示管线的起始位置,但我的解决方案是行不通的,因为我试图用错误的方式使用@static方法。
class Foo3():
class Counter:
method = 'regular'
def __init__(self,func,method:str='regular' ):
self.counter = 0
self.func = func
Counter.method =method
def __call__(self,method,*args, **kwds):
if Counter.method == 'Start':
print('count = 0')
self.counter = 0
if Counter.method == 'regular' :
print('+1')
self.counter += 1
return self.func(*args, **kwds)
#count= Counter()
string = 'this is '
start = True
@Counter
@staticmethod
def newinst(method='regular'):
pass
def __init__(self, name, method:str='regular'):
self.name = name
self.method = method
print(self.ainit(self))
Foo3.newinst(method)
print(self.newinst.counter)
@classmethod
def ainit(cls,inst):
print(cls.string +inst.name)
@classmethod
def getCount(cls,inst):
print(self.newinst.counter)
如果我使用计数器类作为解决方案,那是因为我希望每个Pipeline都有一个计数。
class pipe1(Foo3):
pass
class pipe2(Foo3):
pass
pipe1('test', 'Start')
pipe1('test2')
pipe1('test3')
pipe2('test_new', 'Start')
pipe1.getCount()
pipe2.getCount()
输出预期 :
this is test
1
this is test2
2
this is test3
3
this is test_new
1
3
1
但我得到的是一个问题。
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-440-1710b9b56152> in <module>
54
55 if __name__ == "__main__":
---> 56 Foo3('a')
<ipython-input-440-1710b9b56152> in __init__(self, name, method)
43 self.name = name
44 self.method = method
---> 45 Foo3.newinst(method)
46 print(self.newinst.counter)
47
<ipython-input-440-1710b9b56152> in __call__(self, method, *args, **kwds)
29 print('+1')
30 self.counter += 1
---> 31 return self.func(*args, **kwds)
32
33
TypeError: 'staticmethod' object is not callable
我找到了一个解决方案
class Foo3():
class Counter:
method = 'regular'
def __init__(self,func, method= 'regular'):
self.counter = 0
self.func = func
#print(method)
Counter.method =method
#print(Counter.method)
def __call__(self,*args,method ='regular', **kwds):
if method == 'Start':
self.counter =0
elif method =='regular':
self.counter += 1
else :
raise Exception("this method doesn't exist")
return self.func(*args, **kwds)
#count= Counter()
string = 'this is '
start = True
@Counter
def newinst(method='regular'):
pass
def __init__(self, name, method= 'regular'):
self.name = name
#self.method = method
self.ainit(self)
self.newinst(method=method)
print(self.newinst.counter)
@classmethod
def ainit(cls,inst):
print(cls.string +inst.name)
@classmethod
def getCount(cls,inst):
print(self.newinst.counter)
产出
class pipe1(Foo3):
pass
class pipe2(Foo3):
pass
pipe1('test', 'Start')
pipe1('test2')
pipe1('test3')
pipe2('test_new','Start')
this is test
0
this is test2
1
this is test3
2
this is test_new
0
我的错误来自于我对 decorator
和内 Class
在我的例子中 __init__
计数器的功能在我建立管道之前就已经开始了,然后我不知道如何在参数中传递 "开始",因为我在 "开始 "中设置了。启动 现在我的另一个问题--就像@MisterMiyagi提到的那样--来自于事实上我的 getCount()
函数不应该工作,我最好不使用Counter类,但我还是没有弄清楚如何打开一个。Counter
每例 pipeline
具有继承性 Foo3
而没有在`pipline Class global'参数中指定。