Как автоматически генерировать свойства в классе - PullRequest
0 голосов
/ 04 декабря 2018

Задача

Цель состоит в том, чтобы автоматически построить полный путь + строку имени с указанием пути и имени файла.

Исправлено: благодаря @Patrick Haugh.

Ошибка

Сбой следующего кода

mySource = cls_Source_file.Source_file( )
mySource.file_path = "/Volumes/aqua/data/"
mySource.file_name = "short.rst"
print( "mySource.path_name = %s", mySource.path_name )


$ python laboratory.py 
self._path_name = self._file_path + self._file_name
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'

Определение класса в laboratory.py

class Source_file( object ):
    def __init__(self):
        self._file_name = None
        self._file_path = None
        self._path_name = None

    @property
    def file_name( self ):
        """Name of source file."""
        print( "getter of file_name called: self._file_name = ", self._file_name )
        return self._file_name

    @property
    def file_path( self ):
        """Path (absolute) to source file."""
        print( "getter of file_path called: self._file_path = ", self._file_path )
        return self._file_path

    @property
    def path_name( self ): # https://stackoverflow.com/questions/10381967/how-does-the-python-setter-decorator-work
        """File path and name."""
        print( "getter of file_path called: self._path_name = ", self._path_name )
        return self._path_name

    @file_name.setter
    def file_name( self, value ):
        print("setter of _file_name called: self._file_name = ", value )
        self._file_name = value

    @file_path.setter
    def file_path( self, value ):
        print("setter of _file_path called: self._file_path = ", value )
        self._file_path = value

    @path_name.setter
    def file_path( self, value ):
        self._path_name = self._file_path + self._file_name
        print("setter of _file_path called: self._file_path = ", self._path_name )

Окружающая среда

sys.python = '3.7.0 (default, Jun 28 2018, 07:39:16) \n[Clang 4.0.1 (tags/RELEASE_401/final)]'

Вопрос

Как добавить path_name = file_path + file_name?

...