class stack:
def __init__(self):
self.st = []
def push(self, x):
self.st.append(x)
return self
def pop(self):
return self.st.pop()
누군가 파이썬을 실행할 수없고 언 바운드 오류가 발생하지 않고 stack.push (3)을 실행할 수없는 이유를 말해 줄 수 있습니까? 내가 수행이 메서드를 호출하면 "Unbound method ... instance as first arg"오류가 발생하는 이유는 무엇입니까?
>>> from balance import *
>>> stack.push(3)
Traceback (most recent call last):File "<stdin>", line 1, in <module>
TypeError: unbound method push() must be called with stack instance as first argument (got int instance instead)
>>>
을 다음 그러나 나는이 코드를 작성할 때 오류없이 스택에 밀어 수 있습니다
import sys
k = sys.argv[1]
class stack:
def __init__(self):
self.st = []
def push(self, x):
self.st.append(x)
return self
def pop(self):
return self.st.pop()
def isEmpty(self): #added an empty fucntion to stack class
return self.st == []
def balance(k):
braces = [ ('(',')'), ('[',']'), ('{','}') ] #list of braces to loop through
st = stack() #stack variable
for i in k: #as it iterates through input
#it checks against the braces list
for match in braces:
if i == match[0]: #if left brace put in stack
st.push(i)
elif i == match[1] and st.isEmpty(): #if right brace with no left
st.push(i) #append for condition stateme$
elif i == match[1] and not st.isEmpty() and st.pop() != match[0]:
st.push(i) #if there are items in stack pop
# for matches and push rest to stack
if st.isEmpty(): #if empty stack then there are even braces
print("Yes")
if not st.isEmpty(): #if items in stack it is unbalanced
print("No")
balance(k) #run balance function
이어야합니다. idjaw 및 PEP-8처럼 MixedCase 및 ** 인스턴스 **는 소문자로 항상 ** 클래스 **로 지정하십시오. 그런 다음 여기에서 하듯이 클래스를 인스턴스와 혼동 할 수 없습니다. – smci