Python AttributeError:我定义了一个类,但当我运行它时,它显示对象并没有属性

2024-10-03 23:21:38 发布

您现在位置:Python中文网/ 问答频道 /正文

在GlobalObject.py Python文件中,我定义了一个名为ProjectVariable的类:

import os
import pandas as pd

class ProjectVarible:
    def __init__(self):
        self.cwd = os.getcwd()
        self.InPath = self.cwd + '\\data\\'
        self.OutPath = self.cwd + '\\result\\'
        self.ipoPreQuarter = 11
        self.screenDataPre = 19
        self.assetPoolName = 'AllAStock'
        self.indicator = 'amt'

    def show(self):
        print('current set:')
        print('pastDataLen = ', self.pastDataLen, end=';\t')
        print('recentDataLen = ', self.recentDataLen, end=';\t')
        print('assetPoolName = ', self.assetPoolName, end=';\t')
        print('indicator = ', self.indicator)

def which(series,value):
    DF=pd.DataFrame({'index':range(len(series)),'value':series})
    resultdf=DF.loc[DF['value']==value,:]
    return list(resultdf['index'])

当我创建名为PVVariable的ProjectVariable对象时,InPath属性运行良好:

from GlobalObject import *
PVariable=ProjectVarible()
path=PVariable.InPath
path
Out[143]: 'D:\\PycharmProjects\\Entropy Capital\\Financial Indicator\\Fundamental.2\\data\\'

但当我调用ipoPreQuarter属性时,它不起作用并返回AttributeError:

ipoPreQ=PVariable.ipoPreQuarter
Traceback (most recent call last):
  File "D:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-144-abaa7a33b94f>", line 1, in <module>
    ipoPreQ=PVariable.ipoPreQuarter
AttributeError: 'ProjectVarible' object has no attribute 'ipoPreQuarter'

enter image description here

出了什么问题?如何使用这些属性


Tags: importselfdfvaluedefindicatorendseries