本文介绍了'NoneType'和'float'错误-如何避免NoneType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试构建代码,因此如果结果为NoneType,它将被转换为0.但是,我似乎仍然无法通过None到达下一个函数.为什么会这样?

I've tried to structure my code so if NoneType is a result it will be converted into 0. However, I still seem to get None past through to the next function. Why is that?

get_profit_loss_value_fees中第321行的错误

Error in line 321, in get_profit_loss_value_fees

result = self.get_profit_loss_value() - self.get_fees()

TypeError:-:"NoneType"和"float"的不受支持的操作数类型

TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'

def get_profit_loss_value(self):
    if self.get_exit_cpu() > 0:
        if self.type == 'Long':
            result = self.get_entries().aggregate(
            get_profit_loss_value=Sum('amount', output_field=models.FloatField()
                                           ) * (self.get_exit_cpu() - self.get_entry_cpu()))['get_profit_loss_value']
            return 0 if result is None else result
        elif self.type == 'Short':
            ...
    else:
        return 0

def get_profit_loss_value_fees(self):
    result = self.get_profit_loss_value() - self.get_fees()
    return result


根据要求进行整个回溯:

Traceback (most recent call last):
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\core\handlers\base.py", line 145, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\core\handlers\base.py", line 143, in _get_response
    response = response.render()
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\response.py", line 105, in render
    self.content = self.rendered_content
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\response.py", line 83, in rendered_content
    return template.render(context, self._request)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 171, in render
    return self._render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\defaulttags.py", line 309, in render
    return nodelist.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\defaulttags.py", line 309, in render
    return nodelist.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 986, in render
    output = self.filter_expression.resolve(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 670, in resolve
    obj = self.var.resolve(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 795, in resolve
    value = self._resolve_lookup(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 857, in _resolve_lookup
    current = current()
  File "C:\Users\winkl\tj3\portfolios\models.py", line 321, in get_profit_loss_value_fees
    result = self.get_profit_loss_value() - self.get_fees()
TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'

推荐答案

您总是可以围绕 def get_profit_loss_value(self):

def get_profit_loss_value_or_None(self):
   # All code as now

def get_profit_loss_value(self):
    ret = self.get_profit_loss_value_or_None()
    return 0 if ret is None else ret

这也可以防止将来发生的任何更改,这些更改会导致无法预料的 None 返回.

This may also protect against any future changes which result in unforeseen returns of None.

这篇关于'NoneType'和'float'错误-如何避免NoneType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 14:23