I have a dynamic string based on the current file's extension called "extension"
fileextension = os.path.splitext(file.filename)
extension = fileextension.replace(".","")
self.filetype = "pdf - Adobe Portable Document Format"
self.filetype = "txt - ASCII text file"
Ext = Ext()
Ext.filetype = getattr(Ext, extension)()
You called the instance attribute correctly. To fix your issue do not assign the result to the
ext = Ext() getattr(ext, extension)() print(ext.filetype)
It did not work because you return
txt() method and assigned that to
Note that I relabeled the instance of
ext since it may be not a good idea to override a class definition by its instance.