본문 바로가기
파이썬

파이썬 함수 이름이 파이썬 클래스에서 정의되지 않았습니다.

by º기록 2020. 11. 29.
반응형

나는 비교적 파이썬에 익숙하지 않으며 네임 스페이스에 문제가 있습니다.

class a:
    def abc(self):
        print "haha" 
    def test(self):
        abc()

b = a()
b.abc() #throws an error of abc is not defined. cannot explain why is this so

 

해결 방법

 

test () abc 가 누구인지 알지 못하므로 NameError : global name 'abc'is not defined 이 메시지는 다음과 같은 경우에 발생해야합니다. b.test () 를 호출하면 ( b.abc () 를 호출해도됩니다) 다음과 같이 변경합니다.

class a:
    def abc(self):
        print "haha" 
    def test(self):
        self.abc()  
        # abc()

b = a()
b.abc() #  'haha' is printed
b.test() # 'haha' is printed

 

참조 페이지 https://stackoverflow.com/questions/28805028

 

 

반응형

댓글