2014-07-14 1 views
-1

메소드를 서브 클래스로 그룹화하여 실행할 수 있도록 저장되는 상수 세트 (여기서는 파일 및 일부 메타 데이터)를 정의하는 구조를 만들려고합니다.중첩 된 클래스 메소드 acess 외부 클래스 __init__

class diagnostics(object): 
    # set up some shared attributes which are always constant 
    def __init__(self,root_dir, file, year, doy): 

     self.root_dir = root_dir 
     self.file = file 
     self.year = year 
     self.doy = doy 

    # classes of different methods that 
    # require these attributes from diagnostics.__init__ 

    class io: 
     def loadfile() 
     # loads the file from diagnostics.__init__.file 

그래서 내가 좋아하는 뭔가를 할 수 있도록하려면 : 여기

내 구조

diag = diagnostics('.', 'file01.txt', '2007', '001') 
diag.io.loadfile() # will load the file 

답변

2

diagnostics.io는 네임 스페이스에 기존보다 diagnostics 다른과는 아무하고이 없습니다 확실히 그 어떤 경우와도 관련이 없습니다. 당신이하려는 일에 대해 다시 생각해보십시오.

+0

@James가하는 일은 거의 RequireJS 관용구처럼 보입니다. –

+0

그래서 받아 들일 수 없나요? 그래서 대신 나는 그것들을 전역으로 선언하거나 캡슐화 할 때마다 다시 정의해야 할 것입니다. – James

+0

값이 정적 인 경우 클래스 인스턴스에 값을 저장하는 것이 의미가 없습니다. 그냥 클래스 속성을 사용하십시오. 또한 속성을'io' 클래스에 직접 붙일 수 있습니다. – Alp