Dynamic call to an Instance Variable
<p>I have a dynamic string based on the current file's extension called "extension"</p>

<pre><code>fileextension = os.path.splitext(file.filename)[1]
extension = fileextension.replace(".","")
</code></pre>

<p>Lets say extension = "pdf"</p>

<p>How would I be able to call the Ext.pdf() instance variable below?</p>

<pre><code>class Ext:
    def pdf(self):
        self.filetype = "pdf - Adobe Portable Document Format"
    def txt(self):
        self.filetype = "txt - ASCII text file"
Ext = Ext()
</code></pre>

<p>I have tried:</p>

<pre><code>Ext.filetype = getattr(Ext, extension)()
</code></pre>

<p>But this comes up with a blank entry</p>

<p>You called the instance attribute correctly. To fix your issue do not assign the result to the <code>filetype</code> attribute:</p>

<pre><code>ext = Ext()
getattr(ext, extension)()
print(ext.filetype)
</code></pre>

<p>It did not work because you return <code>None</code> within <code>pdf()</code> and <code>txt()</code> method and assigned that to <code>filetype</code> attribute. </p>

<p>Note that I relabeled the instance of <code>Ext</code> to <code>ext</code> since it may be not a good idea to override a class definition by its instance. </p>

python