당신은 클래스의 속성으로 클래스 John
내부의 변수를 정의해야합니다. 그래서 같이 : 당신은 클래스, a = John()
의 인스턴스를 만들 때
__init__()
실행
class John:
def __init__(self): # You have to have this initialization method, which is called when you do 'John()'
self.foo()
def foo(self):
self.abc = "ABC" # Note the use of 'self.'
a = John()
print(a.abc) # abc is an attribute of 'John', no need to pass through 'foo'
편집 할 수 있습니다. __init__()
안에는 클래스의 기능으로 수행 할 수있는 모든 작업을 수행 할 수 있습니다. 스폿에 속성을 만들거나 (속성 이름 앞에 self.
을 사용) 함수 또는 클래스 메서드를 호출 할 수 있습니다.이 경우 foo
메뚜기, 및 자체를이라고합니다. 따라서,이 라인 :
a = John()
하면, John
클래스의 인스턴스를 생성하는 a
에 할당하고, 속성을 생성 John.abc
John.foo()
을 실행 John.__init__()
을 실행.
AttributeError : 'John'객체의 속성이 'abc' – JanaeUmae
입니다. 대답을 수정하십시오. –
감사합니다. – JanaeUmae