Note that there are some explanatory texts on larger screens.

plurals
  1. PO'Float object is not callable' when dividing class function by a number
    text
    copied!<p>I have a class <code>Portfolio</code> with a method <code>portfolio_risk(self, year)</code>.If I try and divide it by a number I get the error</p> <blockquote> <p>Float object is not callable</p> </blockquote> <p>From my understanding the error is arising due to the parenthesis from the <code>portfolio_risk</code> method but I need to call the method to do the calculation. Is there a different way I could write this code to avoid the error?</p> <pre><code>if Portfolio.portfolio_risk(1)/5 &lt; 7: print('meets criteria') </code></pre> <p>EDIT: I have added some more information below:</p> <p>My class is as follows:</p> <pre><code>class Portfolio(import_data): def __init__(self, number_yrs): self.debt = [0 for i in range(number_yrs)] # the import_csv function just pulls a data table from excel and creates a list # the list is 2D and creates a property called self.sub_portfolio in this class # all the values imported from the csv file are of type float self.import_csv('sub_portfolio') self.debt[0] = self.debt_portfolio def portfolio_risk(self, year): # this sums up the risk column of a portfolio to give total risk for a year self.portfolio_risk = sum(a[0] for a in self.debt[year]) return(self.portfolio_risk) </code></pre> <p>If I create an instance of this class:</p> <pre><code>new_portfolio = Portfolio(5) </code></pre> <p>My Portfolio class is contained in a file portfolio_class.py and the following line works correctly within this file when I test it:</p> <pre><code>print(new_portfolio.portfolio_risk(0)) </code></pre> <p>In another file, analysis.py, I have the following code:</p> <pre><code> nyears = 10 real_portfolio = Portfolio(nyears) for i in range(nyears): if i &gt; 0: # first use last prior year portfolio the_debt_portfolio.debt[i] = the_debt_portfolio.debt[i-1] if real_portfolio.portfolio_risk(i)/ 5 &lt; 7: print('this is within the risk band') </code></pre> <p>I now seem to be getting the error:</p> <pre><code> line 29, in portfolio_risk self.portfolio_risk = sum(a[0] for a in self.portfolio_risk[year]) TypeError: 'int' object is not iterable </code></pre> <p>And not the Float object is not callable error</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload