我正在编写计算代码,我开始测试并遇到问题,在尝试获取答案时弹出错误:
Traceback (most recent call last):
File "G:\LARIS\tests\test_centrifugal_injector.py", line 51, in <module>
test()
File "G:\LARIS\tests\test_centrifugal_injector.py", line 34, in test
print(injector.equivalent_geometric_characteristic_injector)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\iru\AppData\Local\Programs\Python\Python312\Lib\functools.py", line 995, in __get__
val = self.func(instance)
^^^^^^^^^^^^^^^^^^^
File "G:\LARIS\src\centrifugal_injector_calc.py", line 89, in equivalent_geometric_characteristic_injector
return self.geometric_characteristics_injector() / (1 + self.coefficient_friction /
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'float' object is not callable
我该如何修复它?
主要文件:
from dataclasses import dataclass
from functools import cached_property
from abc import ABC, abstractmethod
@dataclass(frozen=True)
class Injector(ABC):
@abstractmethod
def geometric_characteristics_injector(self) -> float:
""""""
raise NotImplementedError
@cached_property
def equivalent_geometric_characteristic_injector(self) -> float:
"""Возвращает эквивалентную геометрическую характеристику"""
return self.geometric_characteristics_injector() / 2.0
@dataclass(frozen=True)
class CentrifugalInjector(Injector):
@cached_property
def geometric_characteristics_injector(self) -> float:
"""Возвращает геометрическую характеристику центробежной форсунки"""
return 2.0
@dataclass(frozen=True)
class ScrewInjector(Injector):
@cached_property
def geometric_characteristics_injector(self) -> float:
"""Возвращает геометрическую характеристику шнековой форсунки"""
return 1.0
from src.centrifugal_injector_calc import CentrifugalInjector, ScrewInjector
def test() -> None:
common = {
"outer_diameter_injector": 5,
"side_wall_thickness_injector": 0.1,
"number_input_tangential_holes": 2,
"diameter_input_tangential_holes": 0.1,
"length_input_tangential_holes": 0.1,
"relative_length_twisting_chamber": 0.1,
"diameter_injector_nozzle": 0.1,
"relative_length_injector_nozzle": 0.1,
"angle_nozzle_axis": 0.1,
"mass_flow_rate": 0.1,
"viscosity": 0.1,
"cross_sectional_area_one_passage_channel": 0.1,
"density_fuel_component_front_injector": 0.1,
"density_combustion_products": 0.1,
"surface_tension_coefficient": 0.1,
}
injector = CentrifugalInjector(**common)
if __name__ == "__main__":
test()
所有的基本计算都在主
Injector
类中进行,剩下的两个负责选择本次计算的配置,这样就不会因为直接调用主类而出现错误,它是抽象的,就像函数一样接收配置数据
在评论中扩展@Thierry Lathuille 的答案:
@dataclass(frozen=True)
class ScrewInjector(Injector):
@cached_property
def geometric_characteristics_injector(self) -> float:
"""Возвращает геометрическую характеристику шнековой форсунки"""
return 1.0
您正在声明一个方法
geometric_characteristics_injector
,实际上应该使用 geometric_characteristics_injector()
来调用该方法。
但是 decorator
@cached_property
使用行为类似于属性的特殊访问器覆盖该方法。所以修饰后,正确的用法是geometric_characteristics_injector
,不带括号。
@cached
装饰器而不是 @cached_property
。