最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Accessing a sibling inner class in initialiser - Stack Overflow

programmeradmin4浏览0评论

Consider the following code, if you will:

class ParentService:

    BASE_DIR = '/some/path'

    class Results(str, Enum):
        RESULT1 = 'ResultOne.xlsx'
        RESULT2 = 'ResultTwo.pdf'
        
        def file_path(self) -> str:
            return os.path.join(self.BASE_DIR, self.value)
        
    class ParentException(Exception):
        def __init__(self, result_type: Results): # Unresolved reference 'Results'
            self.result_type = result_type
            self.msg = f'There is a problem with {result_type}'
            super().__init__(self.msg)
            
    @classmethod
    def file_exists(cls, file: Results):
        if not os.path.exists(file.file_path()):
            raise self.ParentException(result_type=file)

Is it possible – and if it is, what is the correct way – to access the Results inner class Enum in the ParentException initialiser for the purposes of limiting the result_type parameter options?

Thanks!

发布评论

评论列表(0)

  1. 暂无评论