홈>
함수 파이프 라인을 만들고 있는데 각 함수를 인덱싱하기 위해 몇 개의 클래스 인스턴스가 열려 있는지 알아야합니다. 또한 카운트가 한계를 초과하지 않고 동일한 파이프 라인을 여러 번 실행하도록 카운터를 초기화하기 위해 '시작'매개 변수를 0으로 설정해야합니다.
먼저 카운터가 서로 파이프 라인 클래스와 공유되므로 하나의 파이프 라인에서만 잘 작동하는 메소드를 작성합니다.
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
이제 파이프 라인이 시작되는 위치를 나타 내기 위해 카운터 클래스 내에서 시작 메소드를 전달하는 방법을 알아 내려고합니다. 그러나 @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)
카운터 클래스를 솔루션으로 사용한 경우 파이프 라인 당 하나의 카운트를 원하기 때문입니다
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
- 답변 # 1
관련 자료
- python - 사용자 정의 클래스 인스턴스가있는 operatorindex
- javascript - 다른 클래스의 클래스에서 메서드를 호출하는 방법은 무엇입니까? 속성 '테스트'JS를 읽을 수 없습니다
- c# - 클래스 인스턴스로 참조를 반환하는 방법은 무엇입니까?
- swift - 카운터로 dataTask 메서드를 여러 번 호출하는 방법은 무엇입니까?
- java 상속 - 서브 클래스 인스턴스가 생성 될 때 수퍼 클래스 메소드를 호출하는 방법은 무엇입니까?
- android - 조각에서보기 모델 클래스의 인스턴스를 만들 수 없습니다
- firebase - i/flutter (21647) - nosuchmethoderror : 'user'클래스에 인스턴스 getter 'password'가 없습니다
- php - 클래스 인스턴스로 인스턴스화 된 클래스 수준 변수를 어떻게 호출합니까?
- c++ - 템플릿에 전달 된 유형이 int 인 경우에만 클래스 템플릿에 정렬 메서드가 있어야합니다
- swift - addTarget 호출시 UIView 클래스의 "인식되지 않은 선택기가 인스턴스로 전송 됨"
- python - 클래스 인스턴스 생성 문제
- java - kafkaexception - 클래스가 orgapachekafkacommonserializationdeserializer의 인스턴스가 아닙니다
- python - 유형의`__init__` 또는`__new__`를 다른 클래스 인스턴스에 바인딩하는 방법은 무엇입니까?
- java - 인스턴스 변수없이 열거 형 클래스를 만드는 방법은 무엇입니까?
- oop - 파이썬에서 부모 클래스의 각 자식 클래스의 메서드에 데코레이터를 할당하는 방법
- parallel processing - 파이썬에서 클래스 메서드를 병렬화하는 방법은 무엇입니까?
- python - unboundlocalerror - 클래스 메서드를 호출 할 때
- 다른 클래스 Java의 객체로 매개 변수를 사용하여 메서드를 호출하는 방법은 무엇입니까?
- android - MVVM을 사용하는 동안 ViewModel 클래스의 인스턴스를 만들 수 없습니다
- 클래스 (루비)의 메서드에 이름을 표시하는 방법은 무엇입니까?
나는 해결책을 찾는다
산출
내 오류는 약한 지식에 대한 것입니다
decorator
내부Class
내 예에서__init__
파이프 라인을 빌드하기 전에 카운터의 기능이 불안정한 다음 매개 변수에서 '시작'을 전달하는 방법을 알지 못했습니다.초기화함수. @MisterMiyagi가 언급했듯이 이제 내 다른 문제는getCount()
함수가 작동하지 않아야하며 Counter 클래스를 유지하는 것이 좋습니다.하지만 여전히 열린 클래스를 파악하지 못했습니다.Counter
당 인스턴스pipeline
상속으로Foo3
`pipline Class global '매개 변수에 지정하지 마십시오.