2017-04-21 4 views
1

나는 파이썬에서 oops 개념을 배우고 있고, zed 쇼의 작은 CLI 기반 게임을 개발하는 것은 어려운 방법을 배우지 만 오브젝트의 인스턴스 생성에서는 혼란 스럽다.파이썬에서 클래스의 인스턴스화 사이의 차이

코드 :

class animal(object): 
     scenes = { 
      'cat': Cat(), 
      'dog': Dog(), 
      'milk': Milk(), 
      'fight': Fight(), 
      'timeout': Timeout(),} 

     def __init__(self, start_scene): 
      self.start_scene = start_scene 

     def next_scene(self, scene_name): 
      return Map.scenes.get(scene_name) 

     def opening_scene(self): 
      return self.next_scene(self.start_scene) 


    foo = animal('cat') 
    game = run(foo) 
    game.play() 

누군가가 아래 인스턴스화의 차이점은 무엇입니까 설명 할 수 있습니까?

foo = animal()foo = animal('cat')

지금 내가 foo = animal() 클래스 animal의 인스턴스에 foo 설정을 이해하고 foo.opening_scene()

foo = animal('cat')는 무엇을 하는가와 같은 클래스 animal에서 메서드에 액세스 할 수 있습니까?

+0

'cat'은 ** start_scene ** 매개 변수로 전달됩니다. – Prune

답변

1

animal()을 호출하면 기본 구문으로 인스턴스가 만들어집니다. animal ('cat')을 호출하면 실제로는 (self, start_scene)을 호출하여 인스턴스를 만듭니다. 그런 다음 start_scene 속성을 'cat'으로 설정합니다.