简体   繁体   English

将类函数除以数字时,“无法调用浮点对象”

[英]'Float object is not callable' when dividing class function by a number

I have a class Portfolio with a method portfolio_risk(self, year) .If I try and divide it by a number I get the error 我有一个类的Portfolio ,其方法为Portfolio portfolio_risk(self, year) 。如果我尝试将其除以数字,则会出现错误

Float object is not callable 浮动对象不可调用

From my understanding the error is arising due to the parenthesis from the portfolio_risk method but I need to call the method to do the calculation. 据我了解,该错误是由于portfolio_risk方法中的括号引起的,但我需要调用该方法进行计算。 Is there a different way I could write this code to avoid the error? 我可以通过其他方式编写此代码来避免该错误吗?

if Portfolio.portfolio_risk(1)/5 < 7:
    print('meets criteria')

EDIT: I have added some more information below: 编辑:我在下面添加了更多信息:

My class is as follows: 我的课如下:

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)

If I create an instance of this class: 如果我创建此类的实例:

new_portfolio = Portfolio(5)

My Portfolio class is contained in a file portfolio_class.py and the following line works correctly within this file when I test it: 我的投资组合类包含在文件Portfolio_class.py中,当我对其进行测试时,以下行在此文件中可以正常工作:

print(new_portfolio.portfolio_risk(0))

In another file, analysis.py, I have the following code: 在另一个文件analysis.py中,我有以下代码:

 nyears = 10
 real_portfolio = Portfolio(nyears)
 for i in range(nyears):

    if i > 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 < 7:

        print('this is within the risk band')

I now seem to be getting the error: 我现在似乎正在收到错误:

  line 29, in portfolio_risk
  self.portfolio_risk = sum(a[0] for a in self.portfolio_risk[year])
  TypeError: 'int' object is not iterable

And not the Float object is not callable error 而且不是Float对象不可调用错误

There are a couple of problems with your code: 您的代码有两个问题:

  1. If should be if (lowercase i ); If应该是if (小写i );
  2. You need an instance of Portfolio in order to be able to call the (non-static) method. 您需要一个Portfolio的实例,以便能够调用(非静态)方法。

That said, it's not entirely clear how you'd get that exact exception. 就是说,尚不清楚如何获得确切的例外。 One possibility is that somewhere you rebind portfolio_risk to a float . 一种可能性是,你的地方重新绑定portfolio_riskfloat Even then, the capitalized If would produce a syntax error. 即使这样,大写的If也会产生语法错误。

I think the best way forward is for you to fix the two problems mentioned above and, if that doesn't help, post more code. 我认为最好的解决方法是为您解决上述两个问题,如果没有帮助,请发布更多代码。

If the method is defined like this 如果方法是这样定义的

class Portfolio(object):
    def portfolio_risk(self, year):
        return 1

then you must have an instance of Portfolio : 那么您必须有一个Portfolio的实例:

p = Portfolio()
if p.portfolio_risk(1) / 5 < 7:
    # do something

Try something like this: 尝试这样的事情:

portfolio = Portfolio() # you may need to pass some parameters

if portfolio.portfolio_risk(1) / 5 < 7:
    print('meets criteria')

Of course, if portfolio_risk is a field and not a method you'll still get that error. 当然,如果portfolio_risk是一个字段而不是一个方法,您仍然会得到该错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM