AttributeError: module 'ClassMolecule' has no attribute 'save_molecule'

AttributeError: module 'ClassMolecule' has no attribute 'save_molecule'

为了理解 Python 中 classes 的概念,我写了一个带有分子的小程序 'ClassMolecule',我可以在其中定义分子的属性以及将分子属性保存在文件中的方法。

然而,当我使用我的方法将属性保存在文件中时,Python 引发错误,指出我的模块不是我使用的方法,即我的问题标题,据我所知.

ClassMolecule.py

class Molecule:
    """
    Docstring
    """

    def __init__(self, name, T_eb, T_f, m_W, v_m, coefA, coefB, coefC):
        self.name = name
        self.T_eb = T_eb
        ...

    def save_molecule(self):
        with open('molecules_properties.txt', 'a') as f:
            for key, value in self.__dict__.items():
                f.append('%s:%s\n' % (key, value))

在同一文件夹中的另一个文件中,我做了:

import ClassMolecule as CM

    water = CM.Molecule('water', '373', '273', '18', '0.018', '8.07131', '1730.63', '233.426')
    CM.save_molecule(water)

而 python 会引发上述错误。而且我不明白为什么,因为我在 class.

中定义了我的方法

提前感谢您的帮助

调用对象water的方法save_molecule的正确方法是:

water.save_molecule()